Sorting based on Custom Order – Bubble sort
Objective: The need of enhancement is to Sort any field in BOL to perform Sorting with a Custom Order ( Not Alphabetically ).
Example Hypothetical Scenario:
Custom Field (Customer Type) in Search Result View to be Sorted in an arbitrary order so that first Alphabets of description string has order as ( Direct > Internet Sales > Distributor ). This sort order is not alphabetical.
The sorting should be custom in nature & on the relevant field only.
STEPS:
- In Implementation Class of View, Add interface IF_BOL_COL_SORTING.
- Redefine DO_PREPARE_OUTPUT method of implementation class of view & put the logic as
me->typed_context->customers->collection_wrapper->sort(
exporting iv_attr_name = ‘DESCRIPTION’
iv_sort_callback = me
iv_sort_order = ‘D’ ).
- Method IF_BOL_COL_STRING~IS_A_GREATER_B
Because of interface attached to class, a new method is now available i.e,
IF_BOL_COL_SORTING~IS_A_GREATER_B. This method takes the two values IV_A & IV_B to compare & return rv_result as ‘X if IV_A is greater than B else ‘ ‘.
- Sorting
Within this method we can use Simple Application of Bubble sort Algorithm to Sort in a custom order.
Logic |
---|
if value1 = ‘Direct’.
case value2.
when ‘Direct’.
if lv_bu_partner2 > lv_bu_partner1.
rv_result = ‘ ‘.
else.
rv_result = ‘X’.
endif.
when ‘Internet Sales’.
rv_result = ‘X’.
when ‘Distributor’.
rv_result = ‘X’.
when ‘Archive’.
rv_result = ‘ ‘.
when others.
rv_result = ‘X’.
endcase.
endif.
if value1 = ‘Internet Sales’.
case value2.
when ‘Direct’.
rv_result = ‘ ‘.
when ‘Internet Sales’.
if lv_bu_partner2 > lv_bu_partner1.
rv_result = ‘ ‘.
else.
rv_result = ‘ ‘.
endif.
when ‘Distributor’.
rv_result = ‘X’.
when ‘Archive’.
rv_result = ‘ ‘.
when others.
rv_result = ‘X’.
endcase.
endif.
if value1 = ‘Distributor’.
case value2.
when ‘Direct’.
rv_result = ‘ ‘.
when ‘Internet Sales’.
rv_result = ‘ ‘.
when ‘Distributor’.
if lv_bu_partner2 > lv_bu_partner1.
rv_result = ‘ ‘.
else.
rv_result = ‘X’.
endif.
when ‘Archive’.
rv_result = ‘ ‘.
when others.
rv_result = ‘X’.
endcase.
endif. |
We can always Extend this Sorting solution for more fields or number if entries