I'm looking for the easiest way to sort an array that consists of numbers and text, and a combination of these.

\nE.g.

\n`'123asd'\n'19asd'\n'12345asd'\n'asd123'\n'asd12'\n`

\nturns into

\n`'19asd'\n'123asd'\n'12345asd'\n'asd12'\n'asd123'\n`

\nThis is going to be used in combination with the solution to another question I've asked here.

\nThe sorting function in itself works, what I need is a function that can say that that '19asd' is smaller than '123asd'.

\nI'm writing this in JavaScript.

\nEdit: as *adormitu* pointed out, what I'm looking for is a function for natural sorting

I'm trying to make a program that consists of an array of 10 integers which all has a random value, so far so good.

\nHowever, now I need to sort them in order from lowest to highest value and then print it onto the screen, how would I go about doing so?

\n(Sorry for having so much code for a program that small, I ain't that good with loops, just started working with Java)

\n`public static void main(String args[])\n{\n int [] array = new int[10];\n\n array[0] = ((int)(Math.random()*100+1));\n array[1] = ((int)(Math.random()*100+1));\n array[2] = ((int)(Math.random()*100+1));\n array[3] = ((int)(Math.random()*100+1));\n array[4] = ((int)(Math.random()*100+1));\n array[5] = ((int)(Math.random()*100+1));\n array[6] = ((int)(Math.random()*100+1));\n array[7] = ((int)(Math.random()*100+1));\n array[8] = ((int)(Math.random()*100+1));\n array[9] = ((int)(Math.random()*100+1));\n\n System.out.println(array[0] +\" \" + array[1] +\" \" + array[2] +\" \" + array[3]\n +\" \" + array[4] +\" \" + array[5]+\" \" + array[6]+\" \" + array[7]+\" \" \n + array[8]+\" \" + array[9] ); \n\n}\n`

\n","CommentCount":1,"ContentLicense":"CC BY-SA 3.0","CreationDate":"1/20/2012","FavoriteCount":46,"Id":"8938235","LastActivityDate":"2020-12-23T09:27:38.020","LastEditDate":"2017-08-01T04:37:21.803","LastEditorUserId":"815724","OwnerUserId":"1160153","PostTypeId":"1","Score":174,"Tags":"java, arrays","Title":"Sort an array in Java","ViewCount":814433},{"Body":"Well most basic methods are already covered by deceze I would try to look at other types of sort

\n`SplHeap`

`class SimpleHeapSort extends SplHeap {\n public function compare($a, $b) {\n return strcmp($a, $b);\n }\n}\n\n// Let's populate our heap here (data of 2009)\n$heap = new SimpleHeapSort();\n$heap->insert(\"a\");\n$heap->insert(\"b\");\n$heap->insert(\"c\");\n\necho implode(PHP_EOL, iterator_to_array($heap));\n`

\nOutput

\n`c\nb\na\n`

\n`SplMaxHeap`

The SplMaxHeap class provides the main functionalities of a heap, keeping the maximum on the top.

\n`$heap = new SplMaxHeap();\n$heap->insert(1);\n$heap->insert(2);\n$heap->insert(3);\n`

\n`SplMinHeap`

\n\nThe SplMinHeap class provides the main functionalities of a heap, keeping the minimum on the top.

\n

`$heap = new SplMinHeap ();\n$heap->insert(3);\n$heap->insert(1);\n$heap->insert(2);\n`

\nFrom the Wikipedia article on Bubble Sort:

\n\n\nBubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements \"bubble\" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists.

\n

`function bubbleSort(array $array) {\n $array_size = count($array);\n for($i = 0; $i < $array_size; $i ++) {\n for($j = 0; $j < $array_size; $j ++) {\n if ($array[$i] < $array[$j]) {\n $tem = $array[$i];\n $array[$i] = $array[$j];\n $array[$j] = $tem;\n }\n }\n }\n return $array;\n}\n`

\nFrom the Wikipedia article on Selection sort:

\n\n\nIn computer science, selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.

\n

`function selectionSort(array $array) {\n $length = count($array);\n for($i = 0; $i < $length; $i ++) {\n $min = $i;\n for($j = $i + 1; $j < $length; $j ++) {\n if ($array[$j] < $array[$min]) {\n $min = $j;\n }\n }\n $tmp = $array[$min];\n $array[$min] = $array[$i];\n $array[$i] = $tmp;\n }\n return $array;\n}\n`

\nFrom the Wikipedia article on Insertion sort:

\n\n\nInsertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:

\n

`function insertionSort(array $array) {\n $count = count($array);\n for($i = 1; $i < $count; $i ++) {\n\n $j = $i - 1;\n // second element of the array\n $element = $array[$i];\n while ( $j >= 0 && $array[$j] > $element ) {\n $array[$j + 1] = $array[$j];\n $array[$j] = $element;\n $j = $j - 1;\n }\n }\n return $array;\n}\n`

\nFrom the Wikipedia article on Shellsort:

\n\n\nShellsort, also known as Shell sort or Shell's method, is an in-place comparison sort. It generalizes an exchanging sort, such as insertion or bubble sort, by starting the comparison and exchange of elements with elements that are far apart before finishing with neighboring elements.

\n

`function shellSort(array $array) {\n $gaps = array(\n 1,\n 2,\n 3,\n 4,\n 6\n );\n $gap = array_pop($gaps);\n $length = count($array);\n while ( $gap > 0 ) {\n for($i = $gap; $i < $length; $i ++) {\n $tmp = $array[$i];\n $j = $i;\n while ( $j >= $gap && $array[$j - $gap] > $tmp ) {\n $array[$j] = $array[$j - $gap];\n $j -= $gap;\n }\n $array[$j] = $tmp;\n }\n $gap = array_pop($gaps);\n }\n return $array;\n}\n`

\nFrom the Wikipedia article on Comb sort:

\n\n\nComb sort is a relatively simple sorting algorithm originally designed by Wlodzimierz Dobosiewicz in 1980. Later it was rediscovered by Stephen Lacey and Richard Box in 1991. Comb sort improves on bubble sort.

\n

`function combSort(array $array) {\n $gap = count($array);\n $swap = true;\n while ( $gap > 1 || $swap ) {\n if ($gap > 1)\n $gap /= 1.25;\n $swap = false;\n $i = 0;\n while ( $i + $gap < count($array) ) {\n if ($array[$i] > $array[$i + $gap]) {\n // swapping the elements.\n list($array[$i], $array[$i + $gap]) = array(\n $array[$i + $gap],\n $array[$i]\n );\n $swap = true;\n }\n $i ++;\n }\n }\n return $array;\n}\n`

\nFrom the Wikipedia article on Merge sort:

\n\n\nIn computer science, a merge sort (also commonly spelled mergesort) is an O(n log n) comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output

\n

`function mergeSort(array $array) {\n if (count($array) <= 1)\n return $array;\n\n $left = mergeSort(array_splice($array, floor(count($array) / 2)));\n $right = mergeSort($array);\n\n $result = array();\n\n while ( count($left) > 0 && count($right) > 0 ) {\n if ($left[0] <= $right[0]) {\n array_push($result, array_shift($left));\n } else {\n array_push($result, array_shift($right));\n }\n }\n while ( count($left) > 0 )\n array_push($result, array_shift($left));\n\n while ( count($right) > 0 )\n array_push($result, array_shift($right));\n\n return $result;\n}\n`

\nFrom the Wikipedia article on Quicksort:

\n\n\nQuicksort, or partition-exchange sort, is a sorting algorithm developed by Tony Hoare that, on average, makes O(n log n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare.

\n

`function quickSort(array $array) {\n if (count($array) == 0) {\n return $array;\n }\n $pivot = $array[0];\n $left = $right = array();\n for($i = 1; $i < count($array); $i ++) {\n if ($array[$i] < $pivot) {\n $left[] = $array[$i];\n } else {\n $right[] = $array[$i];\n }\n }\n return array_merge(quickSort($left), array(\n $pivot\n ), quickSort($right));\n}\n`

\nFrom the Wikipedia article on Permutation sort:

\n\n\nPermutation sort, which proceeds by generating the possible permutations of the input array/list until discovering the sorted one.

\n

`function permutationSort($items, $perms = array()) {\n if (empty($items)) {\n if (inOrder($perms)) {\n return $perms;\n }\n } else {\n for($i = count($items) - 1; $i >= 0; -- $i) {\n $newitems = $items;\n $newperms = $perms;\n list($foo) = array_splice($newitems, $i, 1);\n array_unshift($newperms, $foo);\n $res = permutationSort($newitems, $newperms);\n if ($res) {\n return $res;\n }\n }\n }\n}\n\nfunction inOrder($array) {\n for($i = 0; $i < count($array); $i ++) {\n if (isset($array[$i + 1])) {\n if ($array[$i] > $array[$i + 1]) {\n return False;\n }\n }\n }\n return True;\n}\n`

\nFrom the Wikipedia article on Radix sort:

\n\n\nIn computer science, radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value.

\n

`// Radix Sort for 0 to 256\nfunction radixSort($array) {\n $n = count($array);\n $partition = array();\n\n for($slot = 0; $slot < 256; ++ $slot) {\n $partition[] = array();\n }\n\n for($i = 0; $i < $n; ++ $i) {\n $partition[$array[$i]->age & 0xFF][] = &$array[$i];\n }\n\n $i = 0;\n\n for($slot = 0; $slot < 256; ++ $slot) {\n for($j = 0, $n = count($partition[$slot]); $j < $n; ++ $j) {\n $array[$i ++] = &$partition[$slot][$j];\n }\n }\n return $array;\n}\n`

\n","CommentCount":6,"ContentLicense":"CC BY-SA 3.0","CreationDate":"6/28/2013","Id":"17364468","LastActivityDate":"2014-05-03T21:38:45.487","LastEditDate":"2017-05-23T12:10:42.083","LastEditorUserId":"-1","OwnerUserId":"1226894","ParentId":"17364127","PostTypeId":"2","Score":140},{"AcceptedAnswerId":"25382196","AnswerCount":14,"Body":"I want to sort a dictionary in Swift. I have a dictionary like:

\n`\"A\" => Array[]\n\"Z\" => Array[]\n\"D\" => Array[]\n`

\netc. I want it to be like

\n`\"A\" => Array[]\n\"D\" => Array[]\n\"Z\" => Array[]\n`

\netc.

\nI have tried many solutions on SO but no one worked for me. I am using XCode6 Beta 5 and on it some are giving compiler error and some solutions are giving exceptions. So anyone who can post the working copy of dictionary sorting.

\n","CommentCount":8,"ContentLicense":"CC BY-SA 3.0","CreationDate":"8/19/2014","FavoriteCount":29,"Id":"25377177","LastActivityDate":"2020-12-29T13:46:21.157","LastEditDate":"2015-11-25T02:23:35.677","LastEditorUserId":"1664443","OwnerUserId":"1409844","PostTypeId":"1","Score":137,"Tags":"swift, sorting, dictionary","Title":"Sort Dictionary by keys","ViewCount":131488},{"Body":"If you are using OS3.0

\nyou can do it like the following

\n`textview.editable = NO;\ntextview.dataDetectorTypes = UIDataDetectorTypeAll;\n`

\n","CommentCount":4,"ContentLicense":"CC BY-SA 2.5","CreationDate":"8/9/2009","Id":"1251727","LastActivityDate":"2009-08-09T16:47:23.627","OwnerDisplayName":"aRrAY","ParentId":"995219","PostTypeId":"2","Score":102}]