-1

I am looking for a solution to get multidimensional arrays naturally sorted with JavaScript. I found some solutions sorting multidimensional arrays or sorting naturally, but I found no solution for both requirements.

I also cannot combine the solutions. here is what the array looks like:

var content = [
{0: 'somename', 1: '10.1.1.100', 2: 'aa-bb-cc-dd-ee-ff'},
{0: 'someothername', 1: '10.1.1.12', 2: '11-22-33-44-55-66'}
];

As you can see the array contains servernames, IP addresses and MAC addresses. It is some table content written in this array. To sort the table I want to sort this array.

Do you have any clever idea how to handle this?

Thank you for your help.

edit: OK maybe, I have to be more specific: There is no problem to sort a multidimensional array. I am able to sort this array for MAC, IP or name. It is a simple one line code. The problem is to sort them naturally. Whene you have alphanumerical strings like server name or IP you want to sort the example list:

{a3, a12, a100, a1} 

like this:

{a1, a3, a12, a100} 

This is called natural sorting. The "normal" sorting instead looks like this:

{a1, a100, a12, a3}

I found several algos for sorting naturally but I was not able to prepare them to sort a multidimenional array.

Hans Wurst
  • 13
  • 1
  • 4
  • 2
    If you provide an example case I am pretty sure within minutes you will either get a duplicate response or many answers. – Redu Dec 05 '16 at 16:09
  • 1
    Would be good to give an example of your array :) – George Dec 05 '16 at 16:10
  • Possible duplicate of [How to sort 2 dimensional array by column value?](http://stackoverflow.com/questions/16096872/how-to-sort-2-dimensional-array-by-column-value) – Liam Dec 05 '16 at 16:25
  • Dont think its a duplicate, cause I need a natural sort. Your example is a normal sort. – Hans Wurst Dec 05 '16 at 16:37
  • First, how is that a multi-dimensional array? It's an array of objects? – jdmdevdotnet Dec 05 '16 at 16:45
  • please add the wanted result as well. – Nina Scholz Dec 05 '16 at 16:45
  • How do you want to sort it? According to the servername, the IP, or the MAC? – Oriol Dec 05 '16 at 16:45
  • I want to choose if it gets sorted by mac, ip or name. The wanted result should be a natural sort - 100 is bigger than 12. When the sort goes for IP, sort should look like this: {0: 'someothername', 1: '10.1.1.12', 2: '11-22-33-44-55-66'}, {0: 'somename', 1: '10.1.1.100', 2: 'aa-bb-cc-dd-ee-ff'} Thank you for all your comments and time. – Hans Wurst Dec 05 '16 at 16:56

1 Answers1

0

It seems trivial to sort according to the properties other than the IP address. If you would like to sort according to the IP addresses you may do as follows;

var ipads = ["192.168.1.1", "192.168.0.1", "10.10.1.1", "178.16.62.42", "42.14.1.1", "42.15.1.1", "178.16.61.42", "192.168.1.2", "0.255.255.255","9.255.255.255","10.0.0.1"],
   result = ipads.map(a => a.split("."))
                 .sort((a,b) => (a[0]-b[0])*16777216 + (a[1]-b[1])*65536 + (a[2]-b[2])*256 + a[3]-b[3])
                 .map(e => e.join("."));
console.log(result);

If you would like to sort with priority of the properties then there are many answers in SO that you can apply this.

Community
  • 1
  • 1
Redu
  • 25,060
  • 6
  • 56
  • 76