Sorting data structures in Python and C#
In this post, we are going to discuss diferent ways of sorting arrays in C# and Python. You’ll find links to the examples to an online IDE container (repl.it)
Python
The sorted()
built-in method sorts any sequence (list, tuple, string, etc) and always returns a
list
with the elements in sorted manner, without modifying the original sequence.
Example:
Another way to sort a list of elements is using the sort()
method. This mehtod only works for
list data structures and it modifies the original list.
Example:
The NumPy library provides you with an array data structure that holds some benefits over Python lists, such as: being more compact, faster access in reading and writing items, being more convenient and more efficient.
The NumPy module has a sort()
method.
Example:
C#
Sort a list of integers
In this example, we will use the List<T>.Sort
Method.
It sorts the elements or a portion of the elements in the List<T>
using either the specified or default IComparer<T>
implementation or a provided Comparison<T>
delegate to compare list elements.
Example:
Sorting an Array of integers
The Sort(Array)
method sorts the elements in an entire one-dimensional Array
using
the IComparable
implementation of each element of the Array
.
In the following example, we are sorting the values in an Array
using the default comparer.