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

Sorting internal table by second highest value

ABAPER_P
Participant
0 Likes
2,106

Sorting internal table by second highest value

How to sort table by second highest value

DOKNR

DOKAR

DOKVR

DOKTL

AENNR

29037

SWD

2

0

5E+11

29037

SWA

1

0

5E+11

29037

SWD

1

0

5E+11

29037

SWA

0

0

5E+11

29037

SWD

0

0

5E+11

I want the DKVR ‘01’ at the first position.

  SELECT  doknr dokar dokvr doktl aennr
FROM draw
INTO TABLE it_draw
WHERE some condition.

10 REPLIES 10
Read only

FredericGirod
Active Contributor
0 Likes
1,717

Hi,

it's not a SORT, it not link with the logical of Ascending or Descending.

You have to make a LOOP and a MODIFY to change the index of the line.

regards

Fred

Read only

Former Member
0 Likes
1,717

I was going to propose a solution but then thought, what is supposed to happen to the record  DKVR ‘02’.

Read only

Former Member
0 Likes
1,717

Hi,

Can you elaborate further and give the output and condition if any?

Read only

Former Member
0 Likes
1,717

Hi,

If you want the 2nd highest. You cant achiev using a normal sort. Instead Loop through the records. like below:

Loop at table.

lv_cnt = lv_cnt + 1.

at new DOKVR.

lv_cnt = lv_cnt + 1.

endat.

When the lv-cnt reaches 2, then move that records to a new table and renumber the rest.

Endloop.

hope this helps.

Thanks,

Naveen

Read only

rosenberg_eitan
Active Contributor
0 Likes
1,717

Hi,

Assuming you want to use your own sort logic .

Add a new column that will be served as a sorting column.

Loop on yor table and change the value of this column to reflect its "importance"

Then sort using this column .


Sample code:

FORM test_06 .

  TYPES: BEGIN OF tp_sflight_1   .
  TYPES index TYPE syindex .
          INCLUDE TYPE sflight .
  TYPES: END OF tp_sflight_1   .

  DATA: it_sflight_1 TYPE TABLE OF tp_sflight_1 .


  SELECT * INTO CORRESPONDING FIELDS OF TABLE it_sflight_1
  FROM sflight UP TO 100 ROWS .

  FIELD-SYMBOLS: <st_sflight_1> LIKE LINE OF it_sflight_1 .

  LOOP AT it_sflight_1 ASSIGNING <st_sflight_1> .


    CASE <st_sflight_1>-planetype .

      WHEN '747-400' .
        <st_sflight_1>-index = 1 .
      WHEN OTHERS .
        <st_sflight_1>-index = 2 .
    ENDCASE.

  ENDLOOP.

  SORT it_sflight_1 BY index planetype .

ENDFORM .                                                   "test_06


Regards.

Read only

0 Likes
1,717

This is easy.

1- Get the internal table and sort by "DOKVR: descending order. This will always bring the highest value on the top.

2- Read the index 1 of this table. THis will return you the highest value.

3- Copy the internal table into another temporary internal table of the same type as 1 above.

4- Delete from internal table 2 where "DOKVR" = value present in fist record.

5- The remaining result is all sorted by the second highest value.

Ashish

Read only

0 Likes
1,717

Hi,

Not really.

The solution that you gave is good for the example that was given .

The solution that I gave is more general .

This code:
  WHEN '747-400' .
        <st_sflight_1>-index = 1 .
      WHEN OTHERS .
        <st_sflight_1>-index = 2 .
    ENDCASE.

Represent a "complex" logic to give the row its position in the list .

Regards.

Read only

former_member206650
Active Participant
0 Likes
1,717

hi SMP,

sort it_draw  ASCENDING by <field>.*sorting*

DELETE it_draw where sy-tabx = '0'.*delete the first entry in the internal table*

hope that it solves ur query....

Read only

former_member206650
Active Participant
0 Likes
1,717

hi SMP,

sort it_draw  ASCENDING by <field>.*sorting*

DELETE it_draw where sy-tabx = '0'.*delete the first entry in the internal table*

hope that it solves ur query....

Read only

0 Likes
1,717

Vishu,

You cannot use sy-tabix = 0, as it may have one or more records.

Ashish