🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: adobe.scripting.indesign
3 messages
3 total messages Started by Rick_Quatro Wed, 28 Jan 2009 02:05
[JS] Sorting an array and removing duplicates
#1722
Author: Rick_Quatro
Date: Wed, 28 Jan 2009 02:05
3 lines
789 bytes
I am using a JavaScript array to store a series of page numbers, which I can store as strings or integers. I am looking for an algorithm to numerically sort the array and remove duplicates. Any suggestions would be appreciated.

Rick Quatro
Re: [JS] Sorting an array and removing duplicates
#1723
Author: Peter Kahrel
Date: Wed, 28 Jan 2009 02:20
21 lines
931 bytes

sort_pages (array)

function sort_pages (array) 
   { 
   var str = array.sort (sortnum).join ('\r')+'\r'; 
   str = str.replace (/([^\r]+\r)(\1)+/g,'$1'); 
   str = str.replace (/\r$/,''); 
   str = str.split ('\r'); 
   return str 
   }

function sortnum (a, b) 
   { 
   return a > b 
   }



Peter
Re: [JS] Sorting an array and removing duplicates
#1730
Author: Rick_Quatro
Date: Wed, 28 Jan 2009 13:23
5 lines
798 bytes
Hi Peter,

Very nice. I had to change the return value on the sortnum function to a - b to get the correct sort order, but otherwise it worked great. Thank you very much for your generous help.

Rick
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads