SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Сортировка символов в строке.



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ABAP
View previous topic :: View next topic  
Author Message
Mania4e11o
Участник
Участник


Age: 33
Joined: 12 Dec 2011
Posts: 47
Location: Рязань

PostPosted: Tue Dec 27, 2011 11:17 pm    Post subject: Сортировка символов в строке. Reply with quote

Задача. Отсортировать строку посимвольно.
Например: BDSTRD -> BDDRST

можно ограничиться только прописными и английскими буквами.

Help. Новичок.
Back to top
View user's profile Send private message Blog
Mania4e11o
Участник
Участник


Age: 33
Joined: 12 Dec 2011
Posts: 47
Location: Рязань

PostPosted: Wed Dec 28, 2011 12:58 am    Post subject: Reply with quote

Решил так.

что можно было сделать, как говорится, красивее?

Code:
DATA: str TYPY string,
            msort TYPE string,
            mi TYPE i,
            mj TYPE i,
            chm TYPE c.
            ch TYPE c.
     
      mi = STRLEN( str ).
      WHILE mi <> 0.

        mi = mi - 1.
        chm = str+mi(1).
        mj = STRLEN( str ) - 1.

        WHILE mj <> 0.
          mj = mj - 1.
          ch = str+mj(1).
          IF  ch < chm.
            chm = ch.
          ENDIF.
        ENDWHILE.

        CONCATENATE msort chm INTO msort.
        REPLACE chm WITH ' ' INTO str.
        CONDENSE str NO-GAPS.

      ENDWHILE.
      WRITE: /10 'Отсорториванная', msort.
Back to top
View user's profile Send private message Blog
Удав
Гуру
Гуру


Age: 48
Joined: 25 Jan 2008
Posts: 580
Location: Москва

PostPosted: Wed Dec 28, 2011 9:58 am    Post subject: Reply with quote

Можно перевести строку в посимвольный массив, отсортировать и присвоить обратно строке.
Code:
TYPES: tt_char TYPE STANDARD TABLE OF char1,
       t_char(30) TYPE c.

DATA: lv_str TYPE t_char VALUE '8yhggfd65za'.
DATA: lt_char TYPE tt_char WITH HEADER LINE.

END-OF-SELECTION.

  WHILE lv_str IS NOT INITIAL.
    lt_char = lv_str(1).
    APPEND lt_char.
    SHIFT lv_str BY 1 PLACES.
  ENDWHILE.
  SORT lt_char.

  LOOP AT lt_char.
    CONCATENATE lv_str lt_char INTO lv_str.
  ENDLOOP.

  WRITE: lv_str.

_________________
С уважением,
Удав.
Back to top
View user's profile Send private message
Удав
Гуру
Гуру


Age: 48
Joined: 25 Jan 2008
Posts: 580
Location: Москва

PostPosted: Wed Dec 28, 2011 10:59 am    Post subject: Reply with quote

Единственная недоработка - нет учета пробелов, но и это можно сделать Wink
_________________
С уважением,
Удав.
Back to top
View user's profile Send private message
Mania4e11o
Участник
Участник


Age: 33
Joined: 12 Dec 2011
Posts: 47
Location: Рязань

PostPosted: Thu Dec 29, 2011 12:12 am    Post subject: Reply with quote

Удав wrote:
...Единственная недоработка - нет учета пробелов, но и это можно сделать .... Wink


Здорово!..) Спасибо, что откликнулся!
Back to top
View user's profile Send private message Blog
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ABAP All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.