Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Unsort a SORTed table.

former_member202771
Contributor
0 Kudos

Hi Experts,

Do we have provision is SAP to unsort a sorted table.

I have used SORT itab by f1 f2.

now after few more changes and modifications on itab, I need the itab in same order before the sort statement but with modified values..

Thanks,

Anil

1 ACCEPTED SOLUTION

rajkumarnarasimman
Active Contributor

Dear Anil,

Unsort option is not available in ABAP.

For this requirement, add the field(slno) in itab(Internal Table) and maintain values in ascending order(From 1 to n) before sorting itself. 

"Sorting before modifications

Sort itab by f1 f2.

After the sorting and modifications done in itab, do the same sorting based on slno as shown below.

"Sorting after modifications - based on Serial Number

Sort itab by slno.

Regards

Rajkumar Narasimman

8 REPLIES 8

Former Member
0 Kudos

Hi Anil.

You can store the ITAB data in 1 more internal table ITAB2 and dont sort it.

When you are modifying the data in ITAB then read the record in itab2 and modify the desired field value in itab2 as well.

Regards,

Sudeesh Soni


rajkumarnarasimman
Active Contributor

Dear Anil,

Unsort option is not available in ABAP.

For this requirement, add the field(slno) in itab(Internal Table) and maintain values in ascending order(From 1 to n) before sorting itself. 

"Sorting before modifications

Sort itab by f1 f2.

After the sorting and modifications done in itab, do the same sorting based on slno as shown below.

"Sorting after modifications - based on Serial Number

Sort itab by slno.

Regards

Rajkumar Narasimman

0 Kudos

Thats good way of solving this. Extend the itab by one component


slno type sy-tabix

To store the line index, before first sort add something like (or maybe you can include this in the routine where you are filling the itab)


field-symbols: <fs> like line of itab.

loop at itab assigning <fs>.

  <fs>-slno = sy-tabix.

endloop.

In the case you can not extend itab directly, you need to create a copy of the itab table if  there is enough ram in the system = could be performance problem with big tables, or create some separated index internal table (little bit more complicated)

0 Kudos

Hi Narasimman & Riha,

That will work beautifully. I would use the solution you suggest, if I was coding in a custom report.

My problem is I want to do this operation in SAPLV03R and there are no enhancement options available at these positions.

SORT output BY isort isort_et and

SORT output STABLE BY matnr

                         werks

                         kunnr

                         vbeln

                         posnr.

.

Any other way in these situations.

Thanks,

Anil

0 Kudos

I believe that in the user exits / enhancements it's better to create a local copy of the global table and then do whatever you need to do with a local copy. If you need to update the global table at the end then it'd probably need to be done in a LOOP. In an enhancement I would imagine there shouldn't be a very large number of records.

There is no "unsort" feature, unfortunately, we can only re-sort using different fields. But can you share with us what are you trying to achieve exactly? Maybe there is no sorting needed to begin with...

ceedee666
Active Contributor
0 Kudos

Hi Anil,

something like that is not possible. How should it be achieved? The system would have to keep track of all sort operations in the background to enable the "unsort" your are asking for.

The question is, what are you really trying to achieve? There is certainly a way to implement you requirements without the magic "unsort" function....

Christian

Former Member
0 Kudos

Hi Anil,

You can follow given logic:

1. Do not sort the Main table-Output

2. Create the new Internal table-Output1 of Output table type adding new field-Sequence.

3. Populate the Output1 table with Output table data and also populate the sequence field.

4. Sort the Output1 table based on required fields.

5. Modify the Output1 table record as per reqirement

6. Sort the Output1 table based on Sequence in ascending.

7. Refresh the Output table and populate it with Output1 table data.

Regards,

Sudeesh Soni

Former Member
0 Kudos

This message was moderated.