首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于按升序对列表值进行排序的函数

用于按升序对列表值进行排序的函数
EN

Stack Overflow用户
提问于 2020-06-16 19:29:58
回答 1查看 411关注 0票数 0

有没有什么函数可以对逗号分隔的列表值进行升序排序?还是有另一种方法呢?Ex- 4,6,8,1输出- 1,4,6,8

EN

Stack Overflow用户

发布于 2020-06-16 20:52:59

没有内置的函数来对分隔列表进行排序。

对于有序的数据集,通常使用带有索引的临时表来支持所需的顺序。

例如(假设整数值按整数顺序排序):

代码语言:javascript
运行
复制
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.

如果出于某种原因,您迫切需要将列表放在分隔字符串中,则可以创建一个函数来从源列表构建临时表,然后将该TT转换为有序列表:

代码语言:javascript
运行
复制
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" ).

如果你真的不想使用临时表,你可以这样做:

代码语言:javascript
运行
复制
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)".
票数 5
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62407415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档