Copy link to clipboard
Copied
Hello Friends,
I have a question, how to sort figure number order?
Example: My Code:
var array1 = ["fig1.1", "fig1.2", "fig1.10", "fig1.15", "fig1.12", "fig1.3"];
bubbleSort(array1);
function bubbleSort(a,b){
var swapped;
do {
swapped = false;
for (var i=0; i < a.length-1; i++) {
if (a > a[i+1])
{
var temp = a;
a = a[i+1];
a[i+1] = temp;
}
}
} while (swapped);
}
alert(array1);
This code current result : fig1.1, fig1.10, fig1.15, fig1.12, fig1.2, fig1.3
I need result: fig1.1, fig1.2, fig1.3, fig1.10, fig1.12, fig1.15
This is possible to sort above order? Please help me friends.
Thanks
SK
Alas, does not work
>> fig1.1,fig1.10,fig1.12,fig1.15,fig1.2,fig1.3
This compares 1.1 against 1.10 and concludes it's the same number! So it's no better than the default plain string comparison.
You need to split off the 'decimal' after the full stop and consider this as a whole integer in itself:
...var array1 = ["fig1.1", "fig1.2", "fig1.10", "fig1.15", "fig1.12", "fig1.3"];
alert (array1.sort(function(a,b)
{
var x = a.match(/\d+/g);
var y = b.match(/\d+/g);
for (var i=0; i<Math.min(x.length, y.length
Copy link to clipboard
Copied
function sortFig (a, b) {
var x = a.replace(/[^.\d]/g,'');
var y = b.replace(/[^.\d]/g,'');
return Number(x) - Number(y) || x.length - y.length;
}
array1.sort (sortFig);
P.
Copy link to clipboard
Copied
Alas, does not work
>> fig1.1,fig1.10,fig1.12,fig1.15,fig1.2,fig1.3
This compares 1.1 against 1.10 and concludes it's the same number! So it's no better than the default plain string comparison.
You need to split off the 'decimal' after the full stop and consider this as a whole integer in itself:
var array1 = ["fig1.1", "fig1.2", "fig1.10", "fig1.15", "fig1.12", "fig1.3"];
alert (array1.sort(function(a,b)
{
var x = a.match(/\d+/g);
var y = b.match(/\d+/g);
for (var i=0; i<Math.min(x.length, y.length); i++)
if (Number(x) != Number(y))
return Number(x) - Number(y);
// give up
return a.localeCompare(b);
}));
Result:
fig1.1,fig1.2,fig1.3,fig1.10,fig1.12,fig1.15
You can even throw in a random "fig1.0" and "fig2.5" and it will still sort correct.
(But I do wonder what this has to do with InDesign.)
Copy link to clipboard
Copied
>> fig1.1,fig1.10,fig1.12,fig1.15,fig1.2,fig1.3
Now that I've opened my eyes. . .
I had noticed that 1.10 and 1.1 are considered the same value, and thought that (after some trial and error) that I had handled it. As you say, you need to split on the decimal. But then a is greater than b if its integer part and its decimal and greater than b's integer part and decimal.
Copy link to clipboard
Copied
https://forums.adobe.com/people/Peter+Kahrel wrote
.. As you say, you need to split on the decimal. But then a is greater than b if its integer part and its decimal and greater than b's integer part and decimal.
(my emphasis) -- If the "integer" part of b is already larger than a's, then you don't need to check any further. And neither when it's *smaller*.
My code creates a list of all consecutive decimals, and when comparing these pair by pair, the first non-match is used. That means that for "1.2" and "1.20", the "2" and "20" count, but when you have "1.999" and "2.0001", the digits after the full stop are never considered at all. The different numbers "1' and "2" can immediately return a valid result.
As it happens, this means that it can also sort any number of levels. It needs one small addition: if the number of levels is not the same, for two entries, you need to supply a default value to compare against. You could use '0' as this would be easier to code, but it's just that little bit nicer to use "-1". With this, you can compare "1.0" against "1" and get the result that the latter is smaller.
var array1 = ["section 1.5.3", "section 2.3.0", "§ 2", "section 5.5", "s. 2.0", "s.5.6"];
alert(array1.sort(function(a,b)
{
var x = a.match(/\d+/g);
var y = b.match(/\d+/g);
if (x.length < y.length)
x.push(-1,-1,-1,-1,-1);
else if (y.length < x.length)
y.push(-1,-1,-1,-1,-1);
for (var i=0; i<Math.min(x.length, y.length); i++)
if (Number(x) != Number(y))
return Number(x) - Number(y);
// give up
return a.localeCompare(b);
}));
Result:
section 1.5.3,§ 2,s. 2.0,section 2.3.0,section 5.5,s.5.6
Copy link to clipboard
Copied
> If the "integer" part of b is already larger than a's, then you don't need to check any further. And neither when it's *smaller*.
Thanks. Time to wake up.
Copy link to clipboard
Copied
Hi Jongware,
Thank you! your code working super
Thanks,
SK
Find more inspiration, events, and resources on the new Adobe Community
Explore Now