Difference between revisions of "SortIndex"

 
(16 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
[[Category:Functions that create lists]]
 
[[Category:Functions that create lists]]
 +
[[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 -->
  
= Description =
+
== SortIndex(d, ''i'') ==
 +
Returns the elements of  «i» re-arranged so that the values of «d» (which must be indexed by «i») are in ascending order.  In the event of a tie, the original order is preserved.
  
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.
+
If «i» is omitted,  «d» must be a one-dimensional array (i.e. a list).  In this case, [[SortIndex]] returns an unindexed list of elements.  Use the one-parameter form only when you want an unindexed result, for example to define an index variable. The one-parameter form does array abstract when a new dimension is added to d.
  
== SortIndex(D) ==
+
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 dimensions as «d», but where each element is the corresponding element in «i» indicating the sort.
  
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. 
+
If «d» has indexes other than «i», each “column” is individually sorted, with the resulting sort order being indexed by the extra dimensions. To obtain the sorted array «d», use this: <code>d[i = Sortindex(d, i)]</code>
  
== SortIndex(D,I) ==
+
== Example ==
  
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.
+
To sort the elements of an index (in ascending order), use
  
= Examples =
+
:<code>SortIndex(I)</code>
  
To sort the elemets of an index (in ascending order):
+
To sort the elements of an array '''A''', along '''I''', use  
  SortIndex( I )
 
  
To sort the elements of an array, A, along I:
+
:<code>A[I = sortIndex(A, I)]</code>
A[I=sortIndex(A,I)]
 
  
To sort an array A (indexed by indexes Row and Col) according to the values in Col='key', use:
+
== Optional parameters ==
  A[Row=SortIndex( A[Col='key'],Row)]
+
===CaseInsensitive===
 +
When sorting text values, values are compared by default in a case-sensitive fashion, with capital letters coming before lower case letters. For example, "Zebra" comes before "apple" in a case-sensitive order. 
  
= Advanced Options =
+
To make the sorting case insensitive, specify the optional parameter <code>caseInsensitive: true</code>:
  
== Text Sensitivity ==
+
:<code>SortIndex(D, caseInsensitive: true)</code>
  
When sorting text values, values are compared by default in a text-sensitive fashion, with capital letters coming before lower case lettersFor example, "Zebra" comes before "apple" in a case-sensitive order.
+
=== Descending ===
 +
The default sort order for [[SortIndex]] is ascending. The descending sort order can be obtained by using: <code>SortIndex(d, descending: true)</code>.
  
In Analytica 4.2 and later, you can control case-sensitivity using:
+
For an array containing only numeric values, the descending sort order can also be obtained as<code>SortIndex(-d)</code>.
  [[SortIndex]](D,caseInsensitive:true)
 
  
In Analytica 4.1 and earlier, use:
+
If 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 and the resulting sort order is heterogeneous.
  [[SortIndex]]([[TextUpperCase]](D))
 
  
== Descending Order ==
+
=== KeyIndex ===
 +
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 descending sort order for an array containing all numeric values is obtained as:
+
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 secondary 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:
  [[SortIndex]](-D)
 
  
If an array contains text values, you can obtain the descending order in Analytica 4.2 or later using:
+
:<code>Index K := ['last', 'first']</code>
[[SortIndex]](D,descending:true)
+
:<code>SortIndex(Array(K, [lastName, firstName]), Person, K)</code>
  
In Analytica 4.1 or earlier, when text values are present, reverse the elements after the sort using:
+
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.
Var r := [[SortIndex]](D) Do [[Slice]](r,[[Size]](r)..1)
 
 
== Multi-Key Sort ==
 
  
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.
+
=== 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'': <code>SortIndex]](D, position: true)</code>
  
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:
+
Using positional notation, the original array can be re-ordered using<code>D[@I = SortIndex(D, I, position: true)]</code>
  [[Index..Do|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.
+
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).
  
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:
+
==Details & More Examples==
[[Index..Do|Index]] order1 := [[SortIndex]](firstname,Person);
+
===Example 1===
[[SortIndex]](lastName[Person=order1],Person)
+
To sort an array '''A''' (indexed by indexes '''Row''' and '''Col''') according to the values in <code>Col = key</code>, use
  
== By Position ==
+
:<code>A[Row = SortIndex(A[Col = 'key'], Row)]</code>
  
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'':
+
===Example 2===
[[SortIndex]](D,position:true)
+
Let:
  
Using positional notation, the original array can be re-ordered using:
+
:<code>Variable Maint_costs :=</code>
D[@I=[[SortIndex]](D,I,position:true)]
+
:{| class="wikitable"
 +
! colspan="3" |Car_type ▶
 +
|-
 +
!'''VW'''
 +
!'''Honda'''
 +
!'''BMW'''
 +
|-
 +
|1950
 +
|1800
 +
|2210
 +
|}
  
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).
+
:<code>Index Sorted_cars := SortIndex(Maint_costs)</code>
 +
 
 +
Then:
 +
 
 +
:<code>SortIndex(Maint_costs, Car_type) &rarr;</code>
 +
:{| class="wikitable"
 +
! colspan="3" |Car_type ▶
 +
|-
 +
!'''VW'''
 +
!'''Honda'''
 +
!'''BMW'''
 +
|-
 +
|Honda
 +
|VW
 +
|BMW
 +
|}
 +
 
 +
:<code>SortIndex(Maint_costs) &rarr;</code>
 +
:{| class="wikitable"
 +
! colspan="3" |SortIndex ▶
 +
|-
 +
|Honda
 +
|VW
 +
|BMV
 +
|}
  
= See Also =
+
:<code>Maint_costs[Car_type = Sorted_cars] &rarr;</code>
 +
:{| class="wikitable"
 +
!Honda
 +
!VW
 +
!BMW
 +
|-
 +
|1800
 +
|1950
 +
|2210
 +
|}
  
 +
== See Also ==
 +
* [[Sort]]
 
* [[Rank]]
 
* [[Rank]]
 
* [[Subscript]]
 
* [[Subscript]]
 +
* [[Unique]]

Latest revision as of 22:14, 11 January 2016


SortIndex(d, i)

Returns the elements of «i» re-arranged so that the values of «d» (which must be indexed by «i») are in ascending order. In the event of a tie, the original order is preserved.

If «i» is omitted, «d» must be a one-dimensional array (i.e. a list). In this case, SortIndex returns an unindexed list of elements. Use the one-parameter form only when you want an unindexed result, for example to define an index variable. The one-parameter form does array abstract when a new dimension is added to d.

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 dimensions as «d», but where each element is the corresponding element in «i» indicating the sort.

If «d» has indexes other than «i», each “column” is individually sorted, with the resulting sort order being indexed by the extra dimensions. To obtain the sorted array «d», use this: d[i = Sortindex(d, i)]

Example

To sort the elements of an index (in ascending order), use

SortIndex(I)

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

A[I = sortIndex(A, I)]

Optional parameters

CaseInsensitive

When sorting text values, values are compared by default in a case-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).

If 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 and the resulting sort order is heterogeneous.

KeyIndex

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 secondary 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).

Details & More Examples

Example 1

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)]

Example 2

Let:

Variable Maint_costs :=
Car_type ▶
VW Honda BMW
1950 1800 2210
Index Sorted_cars := SortIndex(Maint_costs)

Then:

SortIndex(Maint_costs, Car_type) →
Car_type ▶
VW Honda BMW
Honda VW BMW
SortIndex(Maint_costs) →
SortIndex ▶
Honda VW BMV
Maint_costs[Car_type = Sorted_cars] →
Honda VW BMW
1800 1950 2210

See Also

Comments


You are not allowed to post comments.