0

I want to sort array by ascending order of number of characters.

Example array is not sorted.

Array(
    [0] => 1
    [1] => 10
    [2] => 100
    [3] => 101
    [4] => 103
    [5] => 104
    [6] => 105
    [7] => 106
    [8] => 107-B
    [9] => 108
    [10] => 110
    [11] => 111
    [12] => 112
    [13] => 113
    [14] => 114
    [15] => 115
    [16] => 116
    [17] => 117
    [18] => 118
    [19] => 119
    [20] => 12
    [21] => 12-A
    [22] => 120
    [23] => 121
    [24] => 122
    [25] => 123
    [26] => 124
    [27] => 125
    [28] => 126
    [29] => 127
    [30] => 128
    [31] => 129
    [32] => 130
    [33] => 131
    [34] => 132
    [35] => 133
    [36] => 134
    [37] => 135
    [38] => 136
    [39] => 137
    [40] => 138
    [41] => 139
    [42] => 14-A
    [43] => 14-B
    [44] => 140
    [45] => 141
    [46] => 142
    [47] => 143
    [48] => 144
    [49] => 145
    [50] => 146
    [51] => 147
    [52] => 148
    [53] => 149
    [54] => 15
    [55] => 151
    [56] => 152
    [57] => 153
    [58] => 154
    [59] => 155
    [60] => 156-A
    [61] => 158
    [62] => 159
    [63] => 16
    [64] => 160
)

I want like this type of array see below array ...

Array(
    [0] => 1
    [1] => 2
    [2] => 2-A
    [3] => 2-B
    [4] => 3
    [5] => 4
    [6] => 5
    [7] => 10
    [8] => 11
    [9] => 12
    [10] => 12-A
)
0

3 Answers 3

3

Use the case-sensitive natsort or the case-insensitive natcasesort, which will apply the natural order sorting algorithm to your array.

This is your array:

$arr = array(
    "1",     "10",    "100",   "101",   "103",   "104",   "105",   "106",   "107-B", "108",   "110",   "111",   "112",
    "113",   "114",   "115",   "116",   "117",   "118",   "119",   "12",    "12-A",  "120",   "121",   "122",   "123",
    "124",   "125",   "126",   "127",   "128",   "129",   "130",   "131",   "132",   "133",   "134",   "135",   "136",
    "137",   "138",   "139",   "14-A",  "14-B",  "140",   "141",   "142",   "143",   "144",   "145",   "146",   "147",
    "148",   "149",   "15",    "151",   "152",   "153",   "154",   "155",   "156-A", "158",   "159",   "16",    "160"
);

Sort it with either of the two functions mentioned above:

natsort($arr);
print_r($arr);

The output will be:

Array
(
    [0] => 1
    [1] => 10
    [20] => 12
    [21] => 12-A
    [42] => 14-A
    [43] => 14-B
    [54] => 15
    [63] => 16
    [2] => 100
    [3] => 101
    [4] => 103
    [5] => 104
    [6] => 105
    [7] => 106
    [8] => 107-B
    [9] => 108
    [10] => 110
    [11] => 111
    [12] => 112
    [13] => 113
    [14] => 114
    [15] => 115
    [16] => 116
    [17] => 117
    [18] => 118
    [19] => 119
    [22] => 120
    [23] => 121
    [24] => 122
    [25] => 123
    [26] => 124
    [27] => 125
    [28] => 126
    [29] => 127
    [30] => 128
    [31] => 129
    [32] => 130
    [33] => 131
    [34] => 132
    [35] => 133
    [36] => 134
    [37] => 135
    [38] => 136
    [39] => 137
    [40] => 138
    [41] => 139
    [44] => 140
    [45] => 141
    [46] => 142
    [47] => 143
    [48] => 144
    [49] => 145
    [50] => 146
    [51] => 147
    [52] => 148
    [53] => 149
    [55] => 151
    [56] => 152
    [57] => 153
    [58] => 154
    [59] => 155
    [60] => 156-A
    [61] => 158
    [62] => 159
    [64] => 160
)
Sign up to request clarification or add additional context in comments.

Comments

1

natsort — Sort an array using a "natural order" algorithm.

$array=array('09', '8', '10', '009', '011', '0');
print_r(natsort($array));
result:
Array
(
[0] => 0
[1] => 8
[2] => 009
[3] => 09
[4] => 10
[5] => 011
)

1 Comment

The result of your code will be 1. Please read more about the return values of natsort and rewrite your answer.
0

Using natsort seems to work well. There are lots of different PHP sorting functions for tasks like these or others. Coded example & output below:

$test_array = array();
$test_array[0] = '1';
$test_array[1] = '10';
$test_array[2] = '100';
$test_array[3] = '101';
$test_array[4] = '103';
$test_array[5] = '104';
$test_array[6] = '105';
$test_array[7] = '106';
$test_array[8] = '107-B';
$test_array[9] = '108';
$test_array[10] = '110';
$test_array[11] = '111';
$test_array[12] = '112';
$test_array[13] = '113';
$test_array[14] = '114';
$test_array[15] = '115';
$test_array[16] = '116';
$test_array[17] = '117';
$test_array[18] = '118';
$test_array[19] = '119';
$test_array[20] = '12';
$test_array[21] = '12-A';
$test_array[22] = '120';
$test_array[23] = '121';
$test_array[24] = '122';
$test_array[25] = '123';
$test_array[26] = '124';
$test_array[27] = '125';
$test_array[28] = '126';
$test_array[29] = '127';
$test_array[30] = '128';
$test_array[31] = '129';
$test_array[32] = '130';
$test_array[33] = '131';
$test_array[34] = '132';
$test_array[35] = '133';
$test_array[36] = '134';
$test_array[37] = '135';
$test_array[38] = '136';
$test_array[39] = '137';
$test_array[40] = '138';
$test_array[41] = '139';
$test_array[42] = '14-A';
$test_array[43] = '14-B';
$test_array[44] = '140';
$test_array[45] = '141';
$test_array[46] = '142';
$test_array[47] = '143';
$test_array[48] = '144';
$test_array[49] = '145';
$test_array[50] = '146';
$test_array[51] = '147';
$test_array[52] = '148';
$test_array[53] = '149';
$test_array[54] = '15';
$test_array[55] = '151';
$test_array[56] = '152';
$test_array[57] = '153';
$test_array[58] = '154';
$test_array[59] = '155';
$test_array[60] = '156-A';
$test_array[61] = '158';
$test_array[62] = '159';
$test_array[63] = '16';
$test_array[64] = '160';

natsort($test_array);

echo '<pre>';
print_r($test_array);
echo '</pre>';

The output is:

Array
(
    [0] => 1
    [1] => 10
    [20] => 12
    [21] => 12-A
    [42] => 14-A
    [43] => 14-B
    [54] => 15
    [63] => 16
    [2] => 100
    [3] => 101
    [4] => 103
    [5] => 104
    [6] => 105
    [7] => 106
    [8] => 107-B
    [9] => 108
    [10] => 110
    [11] => 111
    [12] => 112
    [13] => 113
    [14] => 114
    [15] => 115
    [16] => 116
    [17] => 117
    [18] => 118
    [19] => 119
    [22] => 120
    [23] => 121
    [24] => 122
    [25] => 123
    [26] => 124
    [27] => 125
    [28] => 126
    [29] => 127
    [30] => 128
    [31] => 129
    [32] => 130
    [33] => 131
    [34] => 132
    [35] => 133
    [36] => 134
    [37] => 135
    [38] => 136
    [39] => 137
    [40] => 138
    [41] => 139
    [44] => 140
    [45] => 141
    [46] => 142
    [47] => 143
    [48] => 144
    [49] => 145
    [50] => 146
    [51] => 147
    [52] => 148
    [53] => 149
    [55] => 151
    [56] => 152
    [57] => 153
    [58] => 154
    [59] => 155
    [60] => 156-A
    [61] => 158
    [62] => 159
    [64] => 160
)

1 Comment

Yes natsort fuction from value getting return "1" not return actual array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.