Copy link to clipboard
Copied
Hi Scripters,
Bored by this code:
var myDoc = app.activeDocument,
mySel = myDoc.selection[0];;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "\\d[\\d\\h,@#]+";
myFound = mySel.findGrep();
var F = myFound.length;
for ( var f = 0 ; f < F ; f++) {
myNewFound = myFound
.contents.split(", ").sort( function (a,b) { return Number (a) > Number (b) } ).join(", "); myFound
.contents = myNewFound; }
app.findGrepPreferences = app.changeGrepPreferences = null;
First case: what I have at the beginning
Second case: what I get with this code
Third case: what I would like to have!
I suppose my problem could be about the line 10 but I don't find a fonction definition to sort as number and as text!
Thanks in advance!
(^/)
Copy link to clipboard
Copied
Hi Obi-wan,
could you weight perhaps "@" and "#" in the order you like to sort them?
Something like that perhaps?
var a1 = ["2","5","12","18","30","2@","3@","19@","2#","3#","18#"];
for(var n=0;n<a1.length;n++)
{
a1
= Number (
a1
. replace(/@$/,".1").
replace(/#$/,".2")
);
};
a1.sort( function(a,b){return Number (a) > Number (b)});
for(var n=0;n<a1.length;n++)
{
a1
= a1 .toString(). replace(/\.1$/,"@").
replace(/\.2$/,"#");
};
a1;
Regards,
Uwe
Copy link to clipboard
Copied
Hi Uwe,
Clever! Thanks! [I take it!]
I've used a similar way:
Add 3 zeros to all numbers, except those that finish by "@" and "#", replaced by, respectively, "001" and "002" (and inverse the process later).
The interest is that I can choose to add "001" to all, except those that finish by "@" and "#", replaced by, respectively, "000" and "002"
==> to sort them differently!
… But I thought [JS] could sort the context I talk about without these turn-around!
First, numbers as "Number()", then last glyph as "String()".
(^/)
Copy link to clipboard
Copied
Hello,
Another solution ...
http://stackoverflow.com/questions/15478954/sort-array-elements-string-with-numbers-natural-sort
var a1 = ["2","5","12","18","30","2@","3@","19@","2#","3#","18#"];
a1.sort(naturalCompare)
alert(a1)
function naturalCompare(a, b) {
var ax = [], bx = [];
a.replace(/(\d+)|(\D+)/g, function(_, $1, $2) { ax.push([$1 || Infinity, $2 || ""]) });
b.replace(/(\d+)|(\D+)/g, function(_, $1, $2) { bx.push([$1 || Infinity, $2 || ""]) });
while(ax.length && bx.length) {
var an = ax.shift();
var bn = bx.shift();
var nn = (an[0] - bn[0]) || an[1].localeCompare(bn[1]);
if(nn) return nn;
}
return ax.length - bx.length;
}
Copy link to clipboard
Copied
Thanks Ronald! I'm going to test it!
Hi Jongware! What do you mean? [It's always easy for you! … Alas, not for me! Aha!]
(^/)
Copy link to clipboard
Copied
It's easy! You only need one single custom sort function!
before: 2,5,12,18,30,2@,3@,19@,2#,3#,18#
after: 2,2@,2#,3@,3#,5,12,18,18#,19@,30
Copy link to clipboard
Copied
var a1 = ["2","5","12","18","30","2@","3@","19@","2#","3#","18#"];
var a2 = a1.slice();a2.sort (function(a,b){
aa = Number(a.match(/\d+/));
bb = Number(b.match(/\d+/));
if (aa != bb)
return aa - bb;aa = [a.match(/\D+/)].join().replace('@','1').replace('#','2');
bb = [b.match(/\D+/)].join().replace('@','1').replace('#','2');
return aa < bb;
});alert ('before: '+a1+'\rafter: '+a2);
(opps, lexicographic sorting does not work with your atypical sorting of '@' before '#' instead of after. Now this also works with "2a" and "2b"!)
Copy link to clipboard
Copied
Thanks Jongware!
Great comments of you 3! Very cool for my "Sorting" Javascript code learning!
I'm going to study all your approaches! [maybe surely some others questions from me!]
About the "@" and "#" use, it's an "only temporary" use in my process. Surely an error from me to use them [a & b could be better]!
The deal is: In an "Index" context, some entries are more important than others, some specific (less important than "basic" entries). So I thought about applying "Bold" to the first group page numbers (here, + blue color to make them more visible) and "Italic" (+ red color) for the second group! As, on the left, after Index génération, on the right, after script.
It's done with my own approach, but I'm really more interested by yours (x3)!
Thanks a lot for your comments!
(^/)