Daby
Daby

Reputation: 1

Function for sorting a list value in ascending order

Is there any function in progress for sorting a comma seperated list value in ascending order? Or there is another way of doing it? Ex- 4,6,8,1 Output- 1,4,6,8

Upvotes: 0

Views: 1117

Answers (1)

Tom Bascom
Tom Bascom

Reputation: 14020

There is no built-in function to sort a delimited list.

For ordered sets of data you would usually use a temp table with an index to support the desired order.

For example (assuming integer values sorted in integer order):

define temp-table numberList
  field number as integer
  index number-idx as primary number
.

create numberList.
numberList.number = 4.

create numberList.
numberList.number = 6.

create numberList.
numberList.number = 8.

create numberList.
numberList.number = 1.

for each numberList:
  display number.
end.

If you desperately need to have your list in a delimited string for some reason you could create a function to build a temp-table from the source list and then convert that TT to an ordered list:

define temp-table numberList
  field number as integer
  index number-idx as primary number
.

function sortList returns character ( input csv as character ):
  define variable n as integer no-undo.
  define variable i as integer no-undo.
  define variable s as character no-undo.
  empty temp-table numberList.
  n = num-entries( csv ).
  do i = 1 to n:
    create numberList.
    numberList.number = integer( entry( i, csv )).
  end.
  for each numberList:
    s = s + string( numberList.number ) + ",".
  end.
  return trim( s, "," ).
end.

display sortList( "4,6,8,1" ).

And if you really just don't want to use temp-tables you could do something like this:

function sortCSV returns character ( input csv as character ):

  define variable n as integer no-undo.    // number of entries in the list
  define variable i as integer no-undo.    // loop counter, number of entries sorted
  define variable j as integer no-undo.    // inner loop counter
  define variable b as integer no-undo.    // temporary minimum
  define variable x as integer no-undo.    // working value
  define variable a as integer no-undo.    // absolute minimum
  define variable z as integer no-undo.    // absolute maximum
  define variable s as character no-undo.  // csv string to return

  n = num-entries( csv ).                  // how many entries in the list?

  a = integer( entry( 1, csv )).           // assume the first entry is the smallest
  z = a.                                   // also assume it is the largest
  do i = 1 to n:
    x = integer( entry( i, csv )).
    a = minimum( a, x ).                   // find the real smallest
    z = maximum( z, x ).                   // and the real largest
  end.

  i = 0.                                   // track the number of sorted entries

  do while i < n:                          // loop until we have sorted all entries

    // add each occurrence of "a" to the sorted list

    do j = 1 to n:
      if integer( entry( j, csv )) = a then
        assign
          i = i + 1                        // increment the count of sorted entries
          s = s + "," + string( a )
        .
    end.

    // look for something smaller than "z"
    // but larger than "a"

    b = z.                                 // reset the temporary minimum to the actual maximum

    do j = 1 to n:                         // scan every entry
      x = integer( entry( j, csv )).
      if x > a then                        // values less than or equal to "a" have already been sorted
        b = minimum( x, b ).
    end.

    a = b.                                 // "a" is ready to add to the list

  end.

  return trim( s, "," ).                   // remove the extra ","

end.

display sortCSV( "4,6,4,-3,8,1" ) format "x(30)".

Upvotes: 5

Related Questions