๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿง‘‍๐Ÿ’ผ SAP/ABAP

[SAP ABAP] SORT์™€ SORT BY ๋ž€? ์˜ค๋ฆ„์ฐจ์ˆœ, ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•

by Jay Din 2024. 1. 8.
728x90
๋ฐ˜์‘ํ˜•

SAP ABAP ์–ธ์–ด์—์„œ `SORT`์™€ `SORT BY`๋Š” ๋‚ด๋ถ€ ํ…Œ์ด๋ธ”์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ์ •๋ ฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋‘ ๊ฐ€์ง€ ๊ตฌ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

SORT ๋ž€?

`SORT`๋Š” ๋‚ด๋ถ€ ํ…Œ์ด๋ธ”์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ์ •๋ ฌํ•˜๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์ •๋ ฌ ๊ตฌ๋ฌธ์ž…๋‹ˆ๋‹ค.

๋ชจ๋“  ํ•„๋“œ์— ๋Œ€ํ•ด ์ •๋ ฌ๋˜๋ฉฐ ๊ธฐ๋ณธ์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.

SORT lt_table.

 

์œ„์˜ ์ฝ”๋“œ์—์„œ `lt_table`์€ ์ •๋ ฌํ•˜๋ ค๋Š” ๋‚ด๋ถ€ ํ…Œ์ด๋ธ”์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

์ด ๊ตฌ๋ฌธ์€ ๋ชจ๋“  ํ•„๋“œ์— ๋Œ€ํ•ด ์˜ค๋ฆ„์ฐจ์ˆœ์„ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

`SORT` ๊ตฌ๋ฌธ์€ ํ…Œ์ด๋ธ”์„ ์ •๋ ฌํ•  ๋•Œ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ๋ชจ๋“  ํ•„๋“œ๋ฅผ ๊ณ ๋ คํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์ œ

DATA: lt_table TYPE TABLE OF string,
      lv_value TYPE string.

* ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
APPEND 'Apple' TO lt_table.
APPEND 'Orange' TO lt_table.
APPEND 'Banana' TO lt_table.
APPEND 'Grapes' TO lt_table.

* ๋‚ด๋ถ€ ํ…Œ์ด๋ธ” ์ •๋ ฌ
SORT lt_table.

* ์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ
LOOP AT lt_table INTO lv_value.
  WRITE: / lv_value.
ENDLOOP.

์‹คํ–‰ํ™”๋ฉด

 

SORT BY ๋ž€?

`SORT BY` ๊ตฌ๋ฌธ์€ ํŠน์ • ํ•„๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

SORT lt_table BY field1 field2.

 

์œ„์˜ ์ฝ”๋“œ์—์„œ `lt_table` ์€ ์ •๋ ฌํ•˜๋ ค๋Š” ๋‚ด๋ถ€ ํ…Œ์ด๋ธ”์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, `field1` ๊ณผ `field2` ๋Š” ์ •๋ ฌ ๊ธฐ์ค€ ํ•„๋“œ์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ํ•„๋“œ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ณ , ๊ฐ ํ•„๋“œ๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋ฉ๋‹ˆ๋‹ค.

`SORT BY`๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ •๋ ฌ ์ˆœ์„œ์™€ ๊ธฐ์ค€ ํ•„๋“œ๋ฅผ ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํŠน์ • ํ•„๋“œ์— ๋Œ€ํ•ด ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•˜๊ณ ์ž ํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์ œ

`COPY_LT_TABLE` ์˜ `NUM` ํ•„๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

TYPES: BEGIN OF LT_TABLE,
         FRUIT TYPE STRING,
         NUM TYPE I,
       END OF LT_TABLE.

DATA: COPY_LT_TABLE TYPE TABLE OF LT_TABLE,
      LV_VALUE TYPE LT_TABLE.

* ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
LV_VALUE-FRUIT = 'Apple'.
LV_VALUE-NUM = 3.
APPEND LV_VALUE TO COPY_LT_TABLE.


LV_VALUE-FRUIT = 'Orange'.
LV_VALUE-NUM = 2.
APPEND LV_VALUE TO COPY_LT_TABLE.


LV_VALUE-FRUIT = 'Banana'.
LV_VALUE-NUM = 1.
APPEND LV_VALUE TO COPY_LT_TABLE.


* ๋‚ด๋ถ€ ํ…Œ์ด๋ธ” ํŠน์ • ํ•„๋“œ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
SORT COPY_LT_TABLE BY NUM.

* ์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ
LOOP AT COPY_LT_TABLE INTO LV_VALUE.
  WRITE: / LV_VALUE-NUM, LV_VALUE-FRUIT.
ENDLOOP.

์‹คํ–‰๊ฒฐ๊ณผ

 

๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ ๋ฐฉ๋ฒ•

๋‚ด๋ฆผ์ฐจ์ˆœ์€ ์•ž์„œ ์„ค๋ช…ํ•œ ์˜ค๋ฆ„์ฐจ์ˆœ์—์„œ `DESCENDING`๋งŒ ์ถ”๊ฐ€ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

SORT ๋‚ด๋ฆผ์ฐจ์ˆœ

* ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋‚ด๋ถ€ ํ…Œ์ด๋ธ” ์ •๋ ฌ
SORT lt_table DESCENDING.

 

SORT BY ๋‚ด๋ฆผ์ฐจ์ˆœ

* ํŠน์ • ํ•„๋“œ๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
SORT lt_table BY table_line DESCENDING.

 

์—ฌ๋Ÿฌ ํ•„๋“œ์˜ ์˜ค๋ฆ„์ฐจ์ˆœ๊ณผ ๋‚ด๋ฆผ์ฐจ์ˆœ ์กฐ๊ฑด

๋‘ํ•„๋“œ์„ ๊ฐ€์ง€๊ณ  SORTํ•˜๋Š”๋ฐ ํ•˜๋‚˜์˜ ํ•„๋“œ๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ ๋‚˜๋จธ์ง€ ํ•„๋“œ๋Š” ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

SORT itab BY field1 ASCENDING field2 DESCENDING.

 

 

728x90
๋ฐ˜์‘ํ˜•