0

I want to sort an array based on a sub array value, that part is fairly simple, but there is an additional requirement.

I also have a known set of values, in which case I want to sort by those instead

In this case the values I am sorting by are IP addresses.

For example, if the address is 1.1.1.5, I want that address to be the first value in the list, and if its 1.1.7.3, I want that to be the second value on the list, and so on.

Then once I am out of known addresses, I want the rest to simply be sorted normally.

The starting order of the array is completely random If possible I also want to append something to the front of the known IPs

How I sort it without the prioritization:

uasort($array, function func($a, $b) {
    return strcmp($a[4], $b[4]);
});
  • 1
    You should add what you've tried to do so far – papafe Aug 19 '20 at 07:15
  • just show us the code you wrote and we will help you. you haven't even mentioned from where we are getting the values. – waanofii.tech Aug 19 '20 at 07:26
  • How can I sort arrays and data in PHP? -> https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php – jspit Aug 19 '20 at 07:40
  • @waanofii.tech I added some code, but the reason I didnt have any is because I dont see how it helps much. I just have a basic sort for now. I dont know how to mix a manual order sort with a standard string comparison sort – Jacob Petersen Aug 19 '20 at 07:58
  • you are the new member so i will assume you are not familiar with how to ask on stack overflow.[read] https://stackoverflow.com/help/how-to-ask moving forward what array do you want us to sort you haven't even posted the array structure let alone the sample data how do you expect to get help of sorting some data without actually providing the data to be excluded and the data to be sorted. the fact that you don't see how it help is even painful than your question. @JacobPetersen – waanofii.tech Aug 19 '20 at 08:29
  • @waanofii.tech I am not here for you to write my code for me, i just want an example of how such a sorting function would look like. – Jacob Petersen Aug 19 '20 at 09:26
  • @JacobPetersen forgive me if i wasted your time trying to help you. one rule of asking here is showing what you have tried. and you don't seem to understand that so farewell. – waanofii.tech Aug 19 '20 at 18:15

1 Answers1

0

I do not think you can change the IP value within the sort so that would need to be done afterwards. How about something like this though for the sort function (you would need to test performance):

$knownIPs = array(
    '1.1.1.5' => 1,
    '1.1.7.3' => 2,
);

$array = array(
    array('f', '0.0.0.0'),
    array('t', '1.1.7.3'),
    array('w', '1.1.1.5'),
    array('r', '1.1.1.6'),
    array('c', '1.1.1.7'),
    array('a', '1.1.1.8'),
    array('i', '1.1.1.9'),
);

uasort($array, function ($a, $b) {
  global $knownIPs;

  $knownIPsKeys = array_keys($knownIPs);
  //if a is a known IP then it has to be before any unknown IP
  if(in_array($a[1],$knownIPsKeys) && !in_array($b[1],$knownIPsKeys)){
    return -1;
  }
  //if b is a known IP then it has to be before any unknown IP
  if(in_array($b[1],$knownIPsKeys) && !in_array($a[1],$knownIPsKeys)){
    return 1;
  }
  //if a and b are both known then check if their order is the same or different
  if(in_array($a[1],$knownIPsKeys) && in_array($b[1],$knownIPsKeys)){
    //if a and b have the same order then compare on the other field as per default
    if($knownIPs[$a[1]]===$knownIPs[$b[1]]){
      return strcmp($a[0], $b[0]);
    }
    //otherwise compare on the order value
    return $knownIPs[$a[1]]<$knownIPs[$b[1]]?-1:1;
  }
  //compare on other field as default
  return strcmp($a[0], $b[0]);
});

print_r($array);

This is a simple script using a global variable to hold the known IPs, there are a number of different ways that $knownIPs can be set up and used depending obviously on the rest of your code but hopefully this gives an idea of how to achieve the sorting.

Mark
  • 1,006
  • 1
  • 6
  • 6