sort array by column which the values are MS. Excel's row tags

Viewed 60

I have this very long list of MS. Excel's row tags.

It's not ordered by column well

$rowTags = [
        'A1',
        'B1',
        'C1',
        'D1',
        'EE42',
        'F1',
        'G1',
        'B2',
        'C2',
        'D2',
        'B3',
        'C3',
        'D3',
        'B4',
        'C4',
        'DD41',
        'B5',
        'C5',
        'D5',
        'B6',
        'C6',
        'D6',
        'B7',
        'C7',
        'D7',
        'B8',
        'D8',
        'B9',
        'DD93',
        'B10',
        'C10',
        'D10',
        'B11',
        'C11',
        'D11',
        'B12',
        'C12',
        'ABC122',
        'B13',
        'C13',
        'AB13',
        'B14',
        'AA52',
        'C14',
        'D14',
        'B15',
        'B16',
        'B17',
        'B18',
        'B19',
        'B20',
        'C20',
        'D20',
        'B21',
        'C21',
        'D21',
        'AA53',
        'C22',
        'D22',
        'C23',
        'D23',
        'C24',
        'D24',
        'C25',
        'C30',
        'D30',
        'C31',
        'D31',
        'C32',
        'D32',
        'C33',
        'D34',
        'DD52',
        'D36',
        'C40',
        'D40',
        'C41',
        'D41',
        'DD62',
        'C42',
        'D42',
        'C43',
        'D43',
        'C44',
        'D44',
    ];

done this with php:

usort($rowTags, function ($item1, $item2) {
    return $item1 <=> $item2;
});

to sort it from the lowest to highest value, expecting that it's ordered by column well, much like columns in Excel. But the fact is the result is still unexpected.

This is an image so you can understand my question better

enter image description here

enter image description here

2 Answers

There might be a duplicate, but just sort naturally:

sort($rowTags, SORT_NATURAL);

You will have to sort column numbers and row numbers both in usort.

The below code will solve the sorting issue.

usort($rowTags, function ($item1, $item2) {
  $col1 = preg_replace('/[0-9]/','', $item1);
  $col2 = preg_replace('/[0-9]/','', $item2);
  $col1len = strlen($col1);
  $col2len = strlen($col2);

  // if first column's length is > second column's length, it needs to be be placed after second column
  if($col1len > $col2len) {
    return 1;
  }
  // if second column's length is > first column's length, it needs to be be placed before first column
  else if($col1len < $col2len) {
    return -1;
  }

  $colCmp = strcmp($col1, $col2);

  // if same column then sort based on row number. i.e. for B5 and B3, we need to look into row number instead
  if($colCmp === 0) {
      $row1 = intval(preg_replace('/[a-zA-Z]/','', $item1));
      $row2 = intval(preg_replace('/[a-zA-Z]/','', $item2));
      return $row1 > $row2;
  }


  // else sort by just normal string comaprison
  return strcmp($col1, $col2);
});

Output of the above code.

Array
(
    [0] => A1
    [1] => A100
    [2] => B1
    [3] => B2
    [4] => B3
    [5] => B4
    [6] => B5
    [7] => B6
    [8] => B7
    [9] => B8
    [10] => B9
    [11] => B10
    [12] => B11
    [13] => B12
    [14] => B13
    [15] => B14
    [16] => B15
    [17] => B16
    [18] => B17
    [19] => B18
    [20] => B19
    [21] => B20
    [22] => B21
    [23] => C1
    [24] => C2
    [25] => C3
    [26] => C4
    [27] => C5
    [28] => C6
    [29] => C7
    [30] => C10
    [31] => C11
    [32] => C12
    [33] => C13
    [34] => C14
    [35] => C20
    [36] => C21
    [37] => C22
    [38] => C23
    [39] => C24
    [40] => C25
    [41] => C30
    [42] => C31
    [43] => C32
    [44] => C33
    [45] => C40
    [46] => C41
    [47] => C42
    [48] => C43
    [49] => C44
    [50] => D1
    [51] => D2
    [52] => D3
    [53] => D5
    [54] => D6
    [55] => D7
    [56] => D8
    [57] => D10
    [58] => D11
    [59] => D14
    [60] => D20
    [61] => D21
    [62] => D22
    [63] => D23
    [64] => D24
    [65] => D30
    [66] => D31
    [67] => D32
    [68] => D34
    [69] => D36
    [70] => D40
    [71] => D41
    [72] => D42
    [73] => D43
    [74] => D44
    [75] => F1
    [76] => G1
    [77] => AA52
    [78] => AA53
    [79] => AB13
    [80] => DD41
    [81] => DD52
    [82] => DD62
    [83] => DD93
    [84] => EE42
    [85] => ABC111
    [86] => ABC122
    [87] => ABC789
    [88] => YAZ99
    [89] => ZZZZ88
)
Related