We use the sort()
method in D to sort an array in an ascending or descending order.
We use the syntax below to sort in ascending order:
array.sort()
We use the syntax below to sort in descending order:
array.sort!("a > b");
This method takes no parameters.
The method returns the array with sorted values according to the specified order.
// import librariesimport std.stdio;import std.algorithm;// main methodvoid main(string[] args) {int[] array = [50, 20, 10, 45, 34]; // create arrayarray.sort(); // sort the arraywriteln(array); // print array elements}
sort()
method.// import librariesimport std.stdio;import std.algorithm;// main methodvoid main(string[] args) {int[] array = [50, 20, 10, 45, 34]; // create arrayarray.sort!("a > b"); // sort the arraywriteln(array); // print array elements}
sort("a > b")
method.