Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Note that it is not valid to sort a list which contains more than one type of data element.

 

list

...

sort_indices(

...

list

...

)

...


list

...

sort_indices(

...

list,string

...

)

Sorts a list and returns the sorted indices. The default behaviour is to sort in ascending order unless an alternative comparison function is provided. See example under sort_and_indices() to see how this function works.

 

list

...

sort_and_indices(

...

list)

...


list

...

sort_and_indices(

...

list,string

...

)

Sorts a list and returns a list of pairs of list items and their corresponding indices in the original list. The default behaviour is to sort in ascending order unless an alternative comparison function is provided. The following example illustrates sort_indices() and sort_and_indices():

original

...

=

...

[10,

...

12,

...

9,

...

7,

...

6]

...


comparison

...

=

...

"<"

...



sorted_list

...

=

...

sort

...

(original,

...

comparison)

...


sorted_indices

...

=

...

sort_indices

...

(original,

...

comparison)

...


sorted_both

...

=

...

sort_and_indices

...

(original,

...

comparison)

...



print

...

('Original

...

list

...

:

...

',

...

original)

...


print

...

('Sorted

...

list

...

:

...

',

...

sorted_list)

...


print

...

('Sorted

...

indices

...

:

...

',

...

sorted_indices)

...


print

...

('Sort

...

and

...

indices

...

:

...

',

...

sorted_both)

...

Note that in this example it is not necessary to provide a comparison operator, as "<" is the default anyway. The output is as follows:

Original

...

list    : [10,12,9,7,6]

...


Sorted

...

list      :

...

[6,7,9,10,12]

...


Sorted

...

indices   :

...

[5,4,3,1,2]

...


Sort

...

and

...

indices

...

:

...

[[6,5],[7,4],[9,3],[10,1],[12,2]]

...