ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Sort with alfa numeric

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sort with alfa numeric

    I got a problem, i got two array, one is the code is numeric and the other is the descripcion.
    the code contens the values(1,2,3).
    so i got this
    datastructure ds
    array 21 dimm(500) ascend
    code 1 0 overlay(array)
    descriptcion 20 overlat(array:*next)

    the values are show in the right way ex:
    1 A
    1 B
    ....
    3 A
    3 B

    when i do sort it stays oky, but since i need to do the sorta descendent i also got
    datastructure2 ds based(abc)
    array2 21 dimm(500) descend
    code2 1 0 overlay(array)
    descriptcion2 20 overlat(array:*next)

    abc s * like(datastrucutere)

    then i got this:

    3 C
    3 B
    ...
    1 B
    1 A

    But what i want is

    3 A
    3 B
    ...
    1 A
    1 B

    I evan trie to multiple the code for -1 to do the sort, but in this case it dosent work becouse the array is alfa and in that case 0003- is bigger than 0002- !

    Any solucion?

  • #2
    Re: Sort with alfa numeric

    Why do you use 2 arrays?
    What you want, you can do it in 1 array with the 2 fields in it!

    Comment


    • #3
      Re: Sort with alfa numeric

      Please send code of ur program.

      Comment


      • #4
        Re: Sort with alfa numeric

        An example:

        I shows you another feature of defining arrays in a data structure.
        The Address array is defined as having 100 elements, each element being 87 characters in length.

        The rest of the data structure appears to define subfields that overlay the first element of the array
        (i.e. the combined length of the subfields is 87 characters).

        But such is not the case; since the subfields overlay an array they are themselves arrays.

        That means operations (such as SortA) executed on a sub field actually affects the overlaid array. In the example shown in Figure 2, the SortA of City results in the Address array being sorted based on the 61 to 80 character of each element.

        Or the SortA of State results in the Address array being sorted based on the 81 and 82 character of each element.

        The %SubArr BIF is used to ensure that only the elements that have been loaded are sorted
        (you do not want blank elements being sorted to the start of the array).

        Code:
         
        D                 DS                  
        D Address                       87a   Dim(100) Ascend    
        D  Street1                      30a   Overlay(Address)
        D  Street2                      30a   Overlay(Address:*Next)
        D  City                         20a   Overlay(Address:*Next)
        D  State                         2a   Overlay(Address:*Next)
        D  Zip                           5a   Overlay(Address:*Next)
        
        D noLoaded        S             10i 0
         /Free
        
          SortA %SubArr(City:1:NoLoaded);     // Sort into City sequence
                                                                         
          SortA %SubArr(State:1:NoLoaded);    // Sort in State sequence

        Comment


        • #5
          Re: Sort with alfa numeric

          I have two arryas becous i need to ordenate the field ascending and descending.
          exemple values in the arrys
          1 A
          1 B
          2 A
          2 B
          3 A
          3 B

          D datastr ds based(abc)
          D array 31 dim(500) ascend
          D code 1 0 overlay(array)
          D desc 30 overlay(array:*next)


          D datastr1 ds
          D array1 31 dim(500) ascend
          D code1 1 0 overlay(array1)
          D desc1 30 overlay(array1:*next)

          D abc s * like(datastrucutere)

          C sorta %subarr(array:1:6)
          I want to do this:

          1 A
          1 B
          2 A
          2 B
          3 A
          3 B

          C sorta %subarr(array1:1:6)
          I want to do this:

          3 A
          3 B
          2 A
          2 B
          1 A
          1 B

          but it is doing this

          3 B
          3 A
          2 B
          2 A
          1 B
          1 A

          Comment


          • #6
            Re: Sort with alfa numeric

            Hi Thunder:

            Excellent question....Hopefully this will provide an answer:
            Code:
            d                 ds                                         
            darray                          21    dim(500) descend       
            D code                           1  0 overlay(array)         
            D desc                          20    overlay(array:*next)   
            darray2                         21    dim(500) ascend        
            D code2                          1  0 overlay(array2)        
            D desc2                         20    overlay(array2:*next)  
            dnbrload                         2s 0 inz(8)                 
            dstart                           2s 0 inz(1)                 
            dend                             2s 0 inz(1)                 
            dnoelem                          2s 0                        
            dfind                            1  0                        
             *                                                           
             /free                                                       
                         code(1)=1;                                      
                         desc(1)='A';                                    
                         desc(1)='A'; 
                         code(2)=1;   
                         desc(2)='B'; 
                         code(3)=2;   
                         desc(3)='A'; 
                         code(4)=2;   
                         desc(4)='B'; 
                         code(5)=3;   
                         desc(5)='A'; 
                         code(6)=3;   
                         desc(6)='B'; 
                         code(7)=4;   
                         desc(7)='A'; 
                         code(8)=4;   
                         desc(8)='B'; 
                         sorta array; 
                         array2=array;
                       dou end > nbrload;                   
                         find = code(start);  
                                // find the next change in code    
                         end = %lookuplt(find:code:start);  
                         noelem = end-start;                
                               // sort either subarr - desc2 or subarr - array2
                         sorta %subarr(desc2:start:noelem); 
                         start=end;                         
                       enddo;                               
                  *inlr = *on;                              
                                                            
             /end-free
            Once code/desc is in descending order, find each change in code and sort on the subarr containing only that code in ascending sequence. at the end of the above array2 should be:

            4A
            4B
            3A
            3B
            2A
            2B
            1A
            1B

            Best of Luck
            GLS
            The problem with quotes on the internet is that it is hard to verify their authenticity.....Abraham Lincoln

            Comment

            Working...
            X