Jump to content

Sort array by version number

Go to solution Solved by Joveice,
$array = array(
    array('id' => 1, 'version' => '1.3'),
    array('id' => 2, 'version' => '1.3.1'),
    array('id' => 3, 'version' => '1.3.2'),
    array('id' => 4, 'version' => '1.3.2-RC1'),
    array('id' => 5, 'version' => '1.3.2-RC2'),
);

usort($array, function($a,$b) {
    return -1 * version_compare ( $a['version'] , $b['version'] );
});

foreach ($array as $ccc) {
	echo $ccc['version'].'<br>';
}

output

1.3.2
1.3.2-RC2
1.3.2-RC1
1.3.1
1.3

See: https://stackoverflow.com/a/48974986/5067386

How can you sort an array based on it's version number?

 

the usort works fine, but I don't see how I can use it on my array.

 

array(
    array('id' => 1, 'version' => '1.3'),
    array('id' => 2, 'version' => '1.3.1'),
    array('id' => 3, 'version' => '1.3.2'),
    array('id' => 4, 'version' => '1.3.2-RC1'),
    array('id' => 5, 'version' => '1.3.2-RC2'),
);

I'm trying to get this order

1.3.2

1.3.2-RC2

1.3.2-RC1

1.3.1

1.3

 

how can I get this to work?

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/900075-sort-array-by-version-number/
Share on other sites

Link to post
Share on other sites

6 minutes ago, Pangea2017 said:

Doesn't work with this

Back-end developer, electronics "hacker"

Link to post
Share on other sites

4 minutes ago, Joveice said:

1.3.2-RC2
1.3.2-RC1
1.3.2
1.3.1
1.3

You probably will have to do something yourself if you want it to sort the way you do. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

1 minute ago, Pangea2017 said:

rsort(array, SORT_NUMERIC);

 

test example: 

<?php

$fruits = array("1", "2", "2-1", "2-2", "3", 1-1);
rsort($fruits, SORT_NUMERIC );


foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}
 

output:

fruits[0] = 3
fruits[1] = 2
fruits[2] = 2-1
fruits[3] = 2-2
fruits[4] = 1
fruits[5] = 0

 

My array is multidimensional

Back-end developer, electronics "hacker"

Link to post
Share on other sites

$array = array(
    '1.3',
    '1.3.1',
    '1.3.2',
    '1.3.2-RC1',
    '1.3.2-RC2',
);

usort($array, 'version_compare');
$array = array_reverse($array);
foreach ($array as $ccc) {
	echo $ccc.'<br>';
}

Output

1.3.2
1.3.2-RC2
1.3.2-RC1
1.3.1
1.3

 

5 minutes ago, djdwosk97 said:

You probably will have to do something yourself if you want it to sort the way you do. 

 

But I can't do this with a multidimensional array.

 

Back-end developer, electronics "hacker"

Link to post
Share on other sites

$array = array(
    array('id' => 1, 'version' => '1.3'),
    array('id' => 2, 'version' => '1.3.1'),
    array('id' => 3, 'version' => '1.3.2'),
    array('id' => 4, 'version' => '1.3.2-RC1'),
    array('id' => 5, 'version' => '1.3.2-RC2'),
);

usort($array, function($a,$b) {
    return -1 * version_compare ( $a['version'] , $b['version'] );
});

foreach ($array as $ccc) {
	echo $ccc['version'].'<br>';
}

output

1.3.2
1.3.2-RC2
1.3.2-RC1
1.3.1
1.3

See: https://stackoverflow.com/a/48974986/5067386

Back-end developer, electronics "hacker"

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×