Difference between revisions of "Sort"

Line 1: Line 1:
 
[[Category:Transforming functions]]
 
[[Category:Transforming functions]]
 
[[Category:Sorting Functions]]
 
[[Category:Sorting Functions]]
 +
[[Category:Array Library]]
 
''New to Analytica 4.2''
 
''New to Analytica 4.2''
  
= Sort(A,I) =
+
==Sort(a, i)==
 
+
Sorts an array «a» in ascending order along index «i». The result has the same indexes as the original array, but the elements are re-ordered.
Sorts an array ''A'' in ascending order along index ''I''.   The result has the same indexes as the original array, but the elements are re-ordered.
 
  
 
=== Comparison to [[SortIndex]] ===
 
=== Comparison to [[SortIndex]] ===
 
 
[[Sort]] is equivalent to:
 
[[Sort]] is equivalent to:
:A[I=[[SortIndex]](A,I)]
+
A[I=[[SortIndex]](A,I)]
  
In several respects, [[SortIndex]] is more general than [[Sort]], but in some cases [[Sort]] is more convenient. [[SortIndex]] provides you with the sorted-order, which can then be used to re-index more than one array. Also, if you are sorting a multi-dimensional array, re-ordering all columns based on the sort-order of one particular "key" column, then you will need to use [[SortIndex]]. When [[SortIndex]](A,I) is applied to a multi-dimensional array, every column is sorted independently.
+
In several respects, [[SortIndex]] is more general than [[Sort]], but in some cases [[Sort]] is more convenient. [[SortIndex]] provides you with the sorted-order, which can then be used to re-index more than one array. Also, if you are sorting a multi-dimensional array, re-ordering all columns based on the sort-order of one particular "key" column, then you will need to use [[SortIndex]]. When [[SortIndex]](A,I) is applied to a multi-dimensional array, every column is sorted independently.
  
 
=== Multi-key sorting ===
 
=== Multi-key sorting ===
 
+
Like [[SortIndex]], [[Sort]] can be used to perform multi-key sorting. With [[Sort]], this is most convenient when the key columns are ordered from left-to-right along the «keyIndex».  With a multi-key sort, the first column of the array (along the «keyIndex») determines the sort order, unless there are ties, in which case the second column breaks the tie. If there is still a tie, then the third column breaks the tie, and so forth. The syntax for using a multi-key sort is:
Like [[SortIndex]], [[Sort]] can be used to perform multi-key sorting.   With [[Sort]], this is most convenient when the key columns are ordered from left-to-right along the ''keyIndex''.  With a multi-key sort, the first column of the array (along the ''keyIndex'') determines the sort order, unless there are ties, in which case the second column breaks the tie. If there is still a tie, then the third column breaks the tie, and so forth. The syntax for using a multi-key sort is:
+
[[Sort]](A,I,keyIndex)
:[[Sort]](A,I,keyIndex)
 
  
 
Note that this syntax provides a convenient way to sort a 2-D array, using the first column to determine the sort order for remaining columns.
 
Note that this syntax provides a convenient way to sort a 2-D array, using the first column to determine the sort order for remaining columns.
  
 
=== Descending sort order ===
 
=== Descending sort order ===
 
+
The optional parameter «descending» reverses the sort order when set to ''True'', e.g.:
The optional parameter ''descending:true'' reverses the sort order, e.g.:
+
[[Sort]](A,I,descending:true)
:[[Sort]](A,I,descending:true)
 
  
 
===Case Sensitivity ===
 
===Case Sensitivity ===
 +
[[Sort]] compares text values in a case-sensitive fashion, with upper case letter coming before lower-case letters in the sort-order. For example, "Zebra" comes before "apple". The optional «caseInsensitive» parameter causes comparisons to occur in a case-insensitive fashion when set to ''True'':
 +
[[Sort]](A,I,caseInsensitive:true)
  
[[Sort]] compares text values in a case-sensitive fashion, with upper case letter coming before lower-case letters in the sort-order.  For example, "Zebra" comes before "apple".  The optional ''caseInsensitive:true'' parameter causes comparisons to occur in a case-insensitive fashion:
 
:[[Sort]](A,I,caseInsensitive:true)
 
 
== Library ==
 
 
Array functions
 
  
 
== Examples ==
 
== Examples ==
 
+
Suppose A is the following table:
Suppose  
 
{|border="0"
 
!     A → ||
 
 
{| border="1"
 
{| border="1"
 
! !!colspan="3" | J
 
! !!colspan="3" | J
 
|-
 
|-
!rowspan="5" | I
+
!rowspan="5" style="width:75px;" | I
| 3 || 2 || 1
+
|style="width:40px;" | 3 ||style="width:75px;" | 2 ||style="width:75px;" | 1
 
|-
 
|-
 
| 1 || 3 || 2
 
| 1 || 3 || 2
Line 53: Line 43:
 
|-
 
|-
 
| 2 || 1 || 2
 
| 2 || 1 || 2
|}
 
 
|}
 
|}
  
{|border="0"
+
Then:
!     [[Sort]](A,I) → ||
+
Sort(A,I) →  
 
{| border="1"
 
{| border="1"
 
! !!colspan="3" | J
 
! !!colspan="3" | J
 
|-
 
|-
!rowspan="5" | I
+
!rowspan="5" style="width:75px;"  | I
| 1 || 1 || 1
+
|style="width:40px;" | 1 ||style="width:75px;" | 1 ||style="width:75px;" | 1
 
|-
 
|-
 
| 1 || 1 || 1
 
| 1 || 1 || 1
Line 71: Line 60:
 
|-
 
|-
 
| 3 || 3 || 2
 
| 3 || 3 || 2
|}
 
 
|}
 
|}
  
{|border="0"
+
And:
!     [[Sort]](A,I,keyIndex:J) → ||
+
[[Sort]](A,I,keyIndex:J) →
 
{| border="1"
 
{| border="1"
 
! !!colspan="3" | J
 
! !!colspan="3" | J
 
|-
 
|-
!rowspan="5" | I
+
!rowspan="5" style="width:75px;"  | I
| 1 || 2 || 1
+
|style="width:40px;" | 1 ||style="width:75px;" | 2 ||style="width:75px;" | 1
 
|-
 
|-
 
| 1 || 3 || 2
 
| 1 || 3 || 2
Line 89: Line 77:
 
|-
 
|-
 
| 3 || 2 || 1
 
| 3 || 2 || 1
|}
 
 
|}
 
|}
  
  
 
== See Also ==
 
== See Also ==
 
 
* [[SortIndex]]
 
* [[SortIndex]]
 
* [[Rank]]
 
* [[Rank]]
  
=User Guide=
 
Sort(x,i) returns the elements of x, reordered along index i in sorted order. The equivalent can be accomplished using the SortIndex function as:
 
x[i=SortIndex(x,i)]
 
  
To perform a multi-key sort, in which the first key determines the sort order unless there are ties, in which the second key breaks the ties, the third key breaks any remaining ties, etc., collect the key criteria along an index K and specify the optional '''keyIndex''' parameter, e.g.:
 
Sort(Array(K,[key1,key2,key3]),i,keyIndex:K)
 
  
The data is sorted in ascending order (from smallest to largest), unless you specify the optional parameter descending:true, which then reorders from largest to smallest. The optional parameter caseInsensitive:true ignores lower/upper case in textual comparisons. Either of these may also be indexed by the keyIndex when the order or case-insensitivity varies by key.
+
===Multi-key example===
 
 
Multi-key example:
 
 
  Sort(NumMaintEvents, CarNum, KeyIndex: MaintType, descending:true)→
 
  Sort(NumMaintEvents, CarNum, KeyIndex: MaintType, descending:true)→
MaintType v, CarNum >
 
  
{| class="wikitable"
+
{| border="1"
 +
! !! colspan="7" style="text-align: left;" | CarNum ▶
 
|-
 
|-
! scope="col"|  
+
! style="width:75px;" | '''MaintType''' ▼
! scope="col"| 1
+
! style="width:75px;" | '''1'''
! scope="col"| 2
+
! style="width:75px;" | '''2'''
! scope="col"| 3
+
! style="width:75px;" | '''3'''
! scope="col"| 4
+
! style="width:75px;" | '''4'''
! scope="col"| 5
+
! style="width:75px;" | '''5'''
! scope="col"| 6
+
! style="width:75px;" | '''6'''
! scope="col"| 7
+
! style="width:75px;" | '''7'''
 
|-
 
|-
 
! scope="row"| Repair
 
! scope="row"| Repair

Revision as of 06:46, 14 August 2015

New to Analytica 4.2

Sort(a, i)

Sorts an array «a» in ascending order along index «i». The result has the same indexes as the original array, but the elements are re-ordered.

Comparison to SortIndex

Sort is equivalent to:

A[I=SortIndex(A,I)]

In several respects, SortIndex is more general than Sort, but in some cases Sort is more convenient. SortIndex provides you with the sorted-order, which can then be used to re-index more than one array. Also, if you are sorting a multi-dimensional array, re-ordering all columns based on the sort-order of one particular "key" column, then you will need to use SortIndex. When SortIndex(A,I) is applied to a multi-dimensional array, every column is sorted independently.

Multi-key sorting

Like SortIndex, Sort can be used to perform multi-key sorting. With Sort, this is most convenient when the key columns are ordered from left-to-right along the «keyIndex». With a multi-key sort, the first column of the array (along the «keyIndex») determines the sort order, unless there are ties, in which case the second column breaks the tie. If there is still a tie, then the third column breaks the tie, and so forth. The syntax for using a multi-key sort is:

Sort(A,I,keyIndex)

Note that this syntax provides a convenient way to sort a 2-D array, using the first column to determine the sort order for remaining columns.

Descending sort order

The optional parameter «descending» reverses the sort order when set to True, e.g.:

Sort(A,I,descending:true)

Case Sensitivity

Sort compares text values in a case-sensitive fashion, with upper case letter coming before lower-case letters in the sort-order. For example, "Zebra" comes before "apple". The optional «caseInsensitive» parameter causes comparisons to occur in a case-insensitive fashion when set to True:

Sort(A,I,caseInsensitive:true)


Examples

Suppose A is the following table:

J
I 3 2 1
1 3 2
2 1 2
1 2 1
2 1 2

Then:

Sort(A,I) → 
J
I 1 1 1
1 1 1
2 2 2
2 2 2
3 3 2

And:

Sort(A,I,keyIndex:J) →
J
I 1 2 1
1 3 2
2 1 2
2 1 2
3 2 1


See Also


Multi-key example

Sort(NumMaintEvents, CarNum, KeyIndex: MaintType, descending:true)→
CarNum ▶
MaintType 1 2 3 4 5 6 7
Repair 10 9 4 4 4 4 1
Scheduled 0 0 5 2 2 1 0
Tires 0 0 0 2 1 0 0
Comments


You are not allowed to post comments.