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: 

Binary search on an internal table which has both ascending and descending order sorted fields

Former Member

Hi,

I have a logic in my program where the internal table is sorted both ascending and descending as shown below:

Sort itab by A B ascending C D descending.

Can I use binary search on this if I read the table only with the first two fields like REAT itab by A B Binary Search? I thought this should work, but it's failing. Can you please help me with this? I know there are too many posts already posted reg. Binary search, but still couldn't find anything very similar to this.

Thanks in advance.

Regards,

Rajarajan.

1 ACCEPTED SOLUTION

matt
Active Contributor

You should use a SORTED table for preference. But if you have to use a standard table, then if you read the documentation about BINARY SEARCH, you'll see it only applies to ascending orders.

7 REPLIES 7

matt
Active Contributor

You should use a SORTED table for preference. But if you have to use a standard table, then if you read the documentation about BINARY SEARCH, you'll see it only applies to ascending orders.

horst_keller
Product and Topic Expert
Product and Topic Expert

"I thought this should work, but it's failing."

What's exactly your problem? The following is working perfectly fine:

DATA(rnd) = cl_abap_random_int=>create( seed = + sy-uzeit
 min = 1
 max = 100 ).
DATA: BEGIN OF line, a TYPE i, b TYPE i, c TYPE i, d TYPE i, END OF line, itab LIKE TABLE OF line WITH EMPTY KEY.
DO 10000 TIMES. itab = VALUE #( BASE itab ( a = rnd->get_next( ) b = rnd->get_next( ) c = rnd->get_next( ) d = rnd->get_next( ) ) ). ENDDO.
SORT itab BY a b ASCENDING c d DESCENDING.
READ TABLE itab INTO DATA(wa) WITH KEY a = 10 b = 10 BINARY SEARCH.
BREAK-POINT.

0 Kudos

I am getting sy-subrc value of '8'. No data is read although entry exists for the Read criteria.

Also the internal table which is READ is of type declared with addition "WITH KEY" on a different field.

Example:

TYPES: begin of i_data,

A type i,

E type i,

C type i,

B type I,

D type i,

End of i_data.

itab type standard table of i_data WITH KEY E.

SORT itab by A B ascending C D descending.

READ itab into wa WITH KEY field1 = A field2 = B BINARY SEARCH.

As Matthew said previously: "if you read the documentation about BINARY SEARCH, you'll see it only applies to ascending orders"

Sandra_Rossi
Active Contributor
0 Kudos

"I thought this should work, but it's failing."

As Horst said, please be factual, what do you expect exactly, what do you get exactly? With an example please!

Jelena
Active Contributor
0 Kudos

In addition to ABAP documentation, you can find some information about binary search in this ancient blog. Hopefully it'll make it clear.

0 Kudos

TCODE - ABAPDOCU says ...

The addition BINARY SEARCH produces a binary search of the table, not linear. In the case of large tables (from approximately 100 entries), this can significantly reduce runtime. The table must, however, be sorted in ascending order by the components specified in the search key. The priority of the sort order must match exactly the order of the components in the search key. If this requirement is not met, the correct row is not usually found.