Difference between revisions of "SortIndex"

(See Also -- Sort)
Line 1: Line 1:
 
[[Category:Functions that create lists]]
 
[[Category:Functions that create lists]]
 
[[Category:Sorting Functions]]
 
[[Category:Sorting Functions]]
 +
[[Category:Array Library]]
 
[[Category:Doc Status C]] <!-- For Lumina use, do not change -->
 
[[Category:Doc Status C]] <!-- For Lumina use, do not change -->
  
Line 26: Line 27:
 
  A[Row=SortIndex( A[Col='key'],Row)]
 
  A[Row=SortIndex( A[Col='key'],Row)]
  
= Advanced Options =
+
<code>Maint_costs →</code>
 +
{| class="wikitable"
 +
!
 +
! colspan="3" |Car_type ▶
 +
|-
 +
!
 +
!'''VW'''
 +
!'''Honda'''
 +
!'''BMW'''
 +
|-
 +
|
 +
|1950
 +
|1800
 +
|2210
 +
|}
 +
<code>SortIndex(Maint_costs, Car_type) →</code>
 +
{| class="wikitable"
 +
!
 +
! colspan="3" |Car_type ▶
 +
|-
 +
!
 +
!'''VW'''
 +
!'''Honda'''
 +
!'''BMW'''
 +
|-
 +
|
 +
|Honda
 +
|VW
 +
|BMW
 +
|}
  
== Text Sensitivity ==
+
<code>SortIndex(Maint_costs) →</code>
 +
{| class="wikitable"
 +
!
 +
! colspan="3" |SortIndex ▶
 +
|-
 +
|
 +
|Honda
 +
|VW
 +
|BMV
 +
|}
  
 +
Define <code>Sorted_cars</code> as an index node:
 +
 +
<code>INDEX Sorted_cars := Sortindex(Maint_costs)</code>
 +
 +
<code>Maint_costs[Car_type = Sorted_cars] →</code>
 +
{| class="wikitable"
 +
!
 +
!Honda
 +
!VW
 +
!BMW
 +
|-
 +
|
 +
|1800
 +
|1950
 +
|2210
 +
|}
 +
 +
== Optional parameters ==
 +
===CaseInsensitive===
 
When sorting text values, values are compared by default in a text-sensitive fashion, with capital letters coming before lower case letters.  For example, "Zebra" comes before "apple" in a case-sensitive order.   
 
When sorting text values, values are compared by default in a text-sensitive fashion, with capital letters coming before lower case letters.  For example, "Zebra" comes before "apple" in a case-sensitive order.   
  
In Analytica 4.2 and later, you can control case-sensitivity using:
+
To make the sorting case insensitive, specify the optional parameter <code>caseInsensitive:true</code>
 
  [[SortIndex]](D,caseInsensitive:true)
 
  [[SortIndex]](D,caseInsensitive:true)
  
In Analytica 4.1 and earlier, use:
+
=== Descending ===
[[SortIndex]]([[TextUpperCase]](D))
+
The default sort order for [[SortIndex]] is ascending. The descending sort order can be obtained by using::
 +
[[SortIndex]](D,descending:true)
  
== Descending Order ==
+
For an array containing only numeric values, the descending sort order can also be obtained as:
 
 
The descending sort order for an array containing all numeric values is obtained as:
 
 
  [[SortIndex]](-D)
 
  [[SortIndex]](-D)
  
If an array contains text values, you can obtain the descending order in Analytica 4.2 or later using:
 
[[SortIndex]](D,descending:true)
 
 
In Analytica 4.1 or earlier, when text values are present, reverse the elements after the sort using:
 
Var r := [[SortIndex]](D) Do [[Slice]](r,[[Size]](r)..1)
 
 
 
== Multi-Key Sort ==
 
== Multi-Key Sort ==
 +
In the event of a tie, [[SortIndex]] preserves the original ordering. A multi-key sort finds the order by sorting on a primary key, but in the event of a tie, breaks the tie using a secondary key.  The pattern can continue to tertiary keys, etc.  In the general case, each key may have a different ascending/descending order or differ on whether comparisons should be case-sensitive.
  
A multi-key sort finds the order by sorting on a primary key, but in the event of a tie, breaking the tie using a secondary key.  The pattern can continue to tertiary keys, etc.  In the general case, each key may have a different ascending/descending order or differ on whether comparisons should be case-sensitive.
+
The values used for the primary key, and the values used for each fall-back key, must all share a common index, ''I''.  To pass these to [[SortIndex]], you must bundle these together along another index, ''keyIndex'', where the first element along ''keyIndex'' is your primary key, the second element is your seconary key, etc.  After you bundle these together, the first parameter to [[SortIndex]] will be a 2-D array indexed by ''I'' and ''keyIndex''.  For example:
 
 
Analytica 4.2 and later has support for multi-key sort built into [[SortIndex]], making multi-key sorting easier than in Analytica 4.1 and earlier.  The values used for the primary key, and the values used for each fall-back key, must all share a common index, ''I''.  To pass these to [[SortIndex]], you must bundle these together along another index, ''keyIndex'', where the first element along ''keyIndex'' is your primary key, the second element is your seconary key, etc.  After you bundle these together, the first parameter to [[SortIndex]] will be a 2-D array indexed by ''I'' and ''keyIndex''.  For example:
 
 
  [[Index..Do|Index]] K := ['last','first'];
 
  [[Index..Do|Index]] K := ['last','first'];
 
  [[SortIndex]]([[Array]](K,[lastName,firstName]), Person, K )
 
  [[SortIndex]]([[Array]](K,[lastName,firstName]), Person, K )
Line 59: Line 109:
 
In this example, we use lastName as the primary key and firstName as the secondary key.  If the optional parameters ''descending'' or ''caseInsensitive'' are also passed, these may optionally be indexed by ''keyIndex'' if the order or case sensitivity varies by key.
 
In this example, we use lastName as the primary key and firstName as the secondary key.  If the optional parameters ''descending'' or ''caseInsensitive'' are also passed, these may optionally be indexed by ''keyIndex'' if the order or case sensitivity varies by key.
  
Multi-key sorts are possible in Analytica 4.1 and earlier as well, but require a bit more work.  Multi-key sorts are accomplished by taking advantage of the fact that the original ordering is preserved in the event of a tie.  Thus, you can sort by the secondary key first, and then sort again using the primary key:
+
=== Position ===
[[Index..Do|Index]] order1 := [[SortIndex]](firstname,Person);
+
By default, [[SortIndex]] returns the elements of the index in the sorted order.  In some cases, you may want the positions of the first element, etc., rather than the index elements (see [[Associative vs. Positional Indexing]]). To obtain the positions, specify the optional parameter ''position:true'':
[[SortIndex]](lastName[Person=order1],Person)
 
 
 
== By Position ==
 
 
 
By default, [[SortIndex]] returns the elements of the index in the sorted order.  In some cases, you may want the positions of the first element, etc., rather than the index elements (see [[Associative vs. Positional Indexing]]). In Analytica 4.2 or better, you can obtain the positions using the optional parameter ''position:true'':
 
 
  [[SortIndex]](D,position:true)
 
  [[SortIndex]](D,position:true)
  
Line 73: Line 118:
 
The use of positional indexing would be required, for example, if you index might contain duplicate values (note that in general, it is very bad style to have duplicate elements in an index).
 
The use of positional indexing would be required, for example, if you index might contain duplicate values (note that in general, it is very bad style to have duplicate elements in an index).
  
= Heterogeneous Sort Order =
+
=== Heterogeneous Sort Order ===
  
 
When the data being sorted contains different data types, the (ascending) sort order used is:  [[Using References|references]], text, [[ParsedExprParameters|parsed expressions]], [[Handle]]s, [[NaN]], numbers, [[Null]], [[Undefined]].  All text values are sorted relative to other text values, and all numbers are sorted relative to other numeric values.  References have no defined sort order, so the ordering among references is arbitrary.
 
When the data being sorted contains different data types, the (ascending) sort order used is:  [[Using References|references]], text, [[ParsedExprParameters|parsed expressions]], [[Handle]]s, [[NaN]], numbers, [[Null]], [[Undefined]].  All text values are sorted relative to other text values, and all numbers are sorted relative to other numeric values.  References have no defined sort order, so the ordering among references is arbitrary.
  
= See Also =
+
== History ==
 +
* Analytica 4.2
 +
** Case-sensitive sort option; in Analytica 4.1 and earlier, use  <code>[[SortIndex]]([[TextUpperCase]](D)) </code>
 +
** Descending sort option; in Analytica 4.1 and earlier, reverse the elements after the sort using <code> Var r := [[SortIndex]](D) Do [[Slice]](r,[[Size]](r)..1)</code>
 +
** Multi-key sorting; in Analytica 4.1 and earlier sort by the secondary key first, and then sort again using the primary key: <code>
 +
[[Index..Do|Index]] order1 := [[SortIndex]](firstname,Person);  [[SortIndex]](lastName[Person=order1],Person) </code>
 +
** Positions of the sorted elements
 +
 
 +
== See Also ==
  
 
* [[Sort]]
 
* [[Sort]]

Revision as of 03:02, 20 November 2015


Description

Returns a permutation of the index elements re-arranged to indicate the ascending sorted ordering of array elements. In the event of a tie, the original order is preserved.

SortIndex(D)

If I is not specified, you should guarantee that D will always be a one-dimensional array. In this case, sortIndex returns an unindexed list of elements.

SortIndex(D,I)

If I is specified, D may be multi-dimensional. Each slice of D is sorted separately along I, with the result being an array having the same dimensionality of D, but where each element is the corresponding element in I indicating the sort.

Examples

To sort the elemets of an index (in ascending order):

SortIndex( I )

To sort the elements of an array, A, along I:

A[I=sortIndex(A,I)]

To sort an array A (indexed by indexes Row and Col) according to the values in Col='key', use:

A[Row=SortIndex( A[Col='key'],Row)]

Maint_costs →

Car_type ▶
VW Honda BMW
1950 1800 2210

SortIndex(Maint_costs, Car_type) →

Car_type ▶
VW Honda BMW
Honda VW BMW

SortIndex(Maint_costs) →

SortIndex ▶
Honda VW BMV

Define Sorted_cars as an index node:

INDEX Sorted_cars := Sortindex(Maint_costs)

Maint_costs[Car_type = Sorted_cars] →

Honda VW BMW
1800 1950 2210

Optional parameters

CaseInsensitive

When sorting text values, values are compared by default in a text-sensitive fashion, with capital letters coming before lower case letters. For example, "Zebra" comes before "apple" in a case-sensitive order.

To make the sorting case insensitive, specify the optional parameter caseInsensitive:true

SortIndex(D,caseInsensitive:true)

Descending

The default sort order for SortIndex is ascending. The descending sort order can be obtained by using::

SortIndex(D,descending:true)

For an array containing only numeric values, the descending sort order can also be obtained as:

SortIndex(-D)

Multi-Key Sort

In the event of a tie, SortIndex preserves the original ordering. A multi-key sort finds the order by sorting on a primary key, but in the event of a tie, breaks the tie using a secondary key. The pattern can continue to tertiary keys, etc. In the general case, each key may have a different ascending/descending order or differ on whether comparisons should be case-sensitive.

The values used for the primary key, and the values used for each fall-back key, must all share a common index, I. To pass these to SortIndex, you must bundle these together along another index, keyIndex, where the first element along keyIndex is your primary key, the second element is your seconary key, etc. After you bundle these together, the first parameter to SortIndex will be a 2-D array indexed by I and keyIndex. For example:

Index K := ['last','first'];
SortIndex(Array(K,[lastName,firstName]), Person, K )

In this example, we use lastName as the primary key and firstName as the secondary key. If the optional parameters descending or caseInsensitive are also passed, these may optionally be indexed by keyIndex if the order or case sensitivity varies by key.

Position

By default, SortIndex returns the elements of the index in the sorted order. In some cases, you may want the positions of the first element, etc., rather than the index elements (see Associative vs. Positional Indexing). To obtain the positions, specify the optional parameter position:true:

SortIndex(D,position:true)

Using positional notation, the original array can be re-ordered using:

D[@I=SortIndex(D,I,position:true)]

The use of positional indexing would be required, for example, if you index might contain duplicate values (note that in general, it is very bad style to have duplicate elements in an index).

Heterogeneous Sort Order

When the data being sorted contains different data types, the (ascending) sort order used is: references, text, parsed expressions, Handles, NaN, numbers, Null, Undefined. All text values are sorted relative to other text values, and all numbers are sorted relative to other numeric values. References have no defined sort order, so the ordering among references is arbitrary.

History

  • Analytica 4.2
    • Case-sensitive sort option; in Analytica 4.1 and earlier, use SortIndex(TextUpperCase(D))
    • Descending sort option; in Analytica 4.1 and earlier, reverse the elements after the sort using Var r := SortIndex(D) Do Slice(r,Size(r)..1)
    • Multi-key sorting; in Analytica 4.1 and earlier sort by the secondary key first, and then sort again using the primary key:
Index order1 := SortIndex(firstname,Person);  SortIndex(lastName[Person=order1],Person) 
    • Positions of the sorted elements

See Also

Comments


You are not allowed to post comments.