Application Development and Automation 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: 
Read only

Reading a sorted table in Sequence

rupali_ray
Employee
Employee
0 Likes
2,801

Hi,

I have a sorted table and want to read it in a sequence.

Say for example:

Table: sort by secondary key field2, non unique

Field1 Field2

10 100

20 100

30 100

Now, when i read the table with the secondray key, field2, I wnat that it should always return the data in same sequence i. e. 100, 200, 300

But currently when the sequence is not maintained liek it can be 100, 200, 300 or 200, 100, 300

Is it posisble that I get the same sequence every time.

Thanks & Regards,

Rupali Ray

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,501

If it is a sorted table, u will always get data in same sequence what ever is the state of ur program.

System will take care that sorting sequence is always maintained.

16 REPLIES 16
Read only

Former Member
0 Likes
2,502

If it is a sorted table, u will always get data in same sequence what ever is the state of ur program.

System will take care that sorting sequence is always maintained.

Read only

0 Likes
2,501

If you want to sort according to field2 than change the columns position in your internal table than you will always get 100 200 300 all the time.

like field2 field1 .

Read only

rupali_ray
Employee
Employee
0 Likes
2,501

Hi,

Correction in the question:

Say for example:

Table: sort by secondary key field2, non unique

Field1 Field2

10 100

20 100

30 100

Now, when i read the table with the secondray key, field2 as 100, I want that it should always return the data in same sequence i. e. 10, 20, 30

But currently the sequence is not maintained like it can be 10, 20, 30 or 20, 10, 30

Is it posisble that I get the same sequence every time.

Read only

0 Likes
2,501

Hi Rupali,

Take the help of KEY word STABLE SORT

for more info please go through the KEY word docu

Regards

Ramchander Rao.K

Read only

0 Likes
2,501

Hi,

you can then sort it by both the keys!!

first sort it by Field2 then field1 as

sort <int_tab> by field2 field1....

it will be in the desired seq.

Regards,

Neha

Read only

0 Likes
2,501

Hi,

stable sort is (in general) used for something different:

If you do a sort on a set of elements and some of these elements are equal regarding the sort function then the elements appear after the sort in the same order as before.

E.g.:

Set of tupels {(3,3), (3,1), (1,1), (2,2)}

Sort function: Sort tuples ascending regarding only the first element

Result (stable):

{(1,1), (2,2), (3,3), (3,1)}

Tupels (3,1) and (3,3) are equal regarding the first element.

In the case Rupali describes, there is no sort statement but a secondary key. I guess (I am not sure), that the secondary key index is not built with respect to a stable sort order based on the order of the primary key.

Regards,

Torsten

Read only

0 Likes
2,501

Hi,

i really didn't realize that, Thanks for reminding me the basics.

Regards

Ramchander Rao.K

Read only

0 Likes
2,501

You're welcome!

Read only

0 Likes
2,501

Hi,

My issue is still not solved. The table is already sorted, so no point in using sort stable.

Can anyone please let me know how can the issue be solved.

Thanks & Regards,

Rupali Ray

Read only

Former Member
0 Likes
2,501

Hi,

When you sort the table sort that table using the two fields that to in ascending order..

this will make the internal table records in sequence like

field1 field2

10 100

20 100

30 100

10 200

20 200

30 200

Now whilw reading the table using the key 2 as 100 or 200 the value of field1 will be in sequence as desired.

ie.... field1 field2

10 100

20 100

30 100

Hope this will help.

Regards,

Rohan.

Edited by: Rohan on Dec 4, 2008 1:02 PM

Read only

0 Likes
2,501

Hi,

The problem i am facing is that sequence of field1 is not same always:

field1 field2

10 100

20 100

30 100

10 200

20 200

30 200

Now while reading the table using the key 2 as 100 or 200 the value of field1 is not in sequence.

Its coming as 10, 20 , 30 or 20, 10, 30

i want a stable sequence.

Thanks & Regards,

Rupali Ray

Read only

Former Member
0 Likes
2,501

Hi Rupali,

As suggested by Ramchander, try the following stmnt, it should work as u want.

SORT ITAB STABLE BY FIELD2.

Hope will solve your problem.

Regards,

Pankaj

Read only

0 Likes
2,501

Hi,

iTAB is already sorted. while creating the table, i had decalred it as sorted table.

Why should i sort again the sorted table?

Thanks & Regards,

Rupali Ray

Read only

0 Likes
2,501

Hi,

Ok then dont stick on the sorted table ...while declaring the table make that as standard table and then after filling up that table sort it,as i said earlier...this will solve ur problem.

Regards,

Rohan.

Read only

0 Likes
2,501

Then include the first column also in sort criterion while defining it as a sort table, e.g.

TYPES ITAB TYPE SORTED TABLE OF mara WITH KEY matnr ernam.

then it will also work for u.

Regards,

Pankaj

Read only

Former Member
0 Likes
2,501

field1 field2

10 100

20 100

30 100

10 200

20 200

30 200

-


if possible move the itab to itab_temp

delete adjacent duplicates from itab_temp comparing field2.