Im new to javascript but I have already made some scripts that really make a difference in my workflow. However I am now embarking on a project that forces me to sort data in a way I dont know howto do in Javascript. I will try to explain what I need to do as if my data was in excel but it isnt, I have only been able to put the data in 4 different arrays:

 pagenumber[1,2,3,4,5] //only numbers
 zipcode[77889,99887,33667,11122,44559] // only numbers
 streetname[Hillroad, Hillroad, Baghdad Street, Hongway, Chinatown] //only letters
 roadnumber[55,27,1,13,16] //only numbers

I would like to sort them like this, first by the zipcode, then by the roadname, then by the even roadnumbers descending, then by the odd roadnumbers ascending. According to this new sorting I want to generate a new pagenumber but I want it to somehow relate to the (old) variable "pagenumber" so I can locate the old page and extract it to a new document with new pagenumbers. I am not asking you guys to write all the code for me but I need a little bit of advice to know firstly if it is possible to do which I think it is, secondly if it is right of me to put the data in four different arrays, thirdly if ther is any (ofcourse) smarter way to save the data so they relate to eachother more closely. Give me your thoughts. Also tips of where and what I should read is appreciated. Thank you all for the answers. However I want to point out that I write my code in Acrobat DC not for the web.

share|improve this question
    
what have you done so far in terms of sorting any of those arrays?? Please show us – HenryDev 24 mins ago
    
I havent sorted it at all. Dont know howto. But I will! – Heresh 20 mins ago
    
if each index in each array refers to a single entity ... i.e. index 0 = 1, 77889, Hillroad, 55 etc, so when sorting one array you need to also reorder the other arrays, then you have about the least efficient way of storing your data that I could think of! – Jaromanda X 9 mins ago

You can use Defiant.js library, which enables you to perform lightning-fast searches on JSON using XPath expressions, and transform JSON using XSL.

share|improve this answer
    
Thank you but I cant since I am working inside Acrobat DC – Heresh 8 mins ago

You can try the following function to sort the text element to sort it in alphabetical order

function sortUnorderedList(ul, sortDescending) {
  if(typeof ul == "string")
    ul = document.getElementById(ul);

  // Idiot-proof, remove if you want
  if(!ul) {
    alert("The UL object is null!");
    return;
  }

  // Get the list items and setup an array for sorting
  var lis = ul.getElementsByTagName("LI");
  var vals = [];

  // Populate the array
  for(var i = 0, l = lis.length; i < l; i++)
    vals.push(lis[i].innerHTML);

  // Sort it
  vals.sort();

  // Sometimes you gotta DESC
  if(sortDescending)
    vals.reverse();

  // Change the list on the page
  for(var i = 0, l = lis.length; i < l; i++)
    lis[i].innerHTML = vals[i];
}

To call this function just use the following function

sortUnorderedList("ID_OF_LIST");

If you are implementing Jquery in your code then you may also try the following

var mylist = $('#myUL');
var listitems = mylist.children('li').get();
listitems.sort(function(a, b) {
   return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
})
$.each(listitems, function(idx, itm) { mylist.append(itm); });

For reference of Jquery code here is link

share|improve this answer
    
Thank you but I am not working on the web. – Heresh 6 mins ago

I suppose the items in your arrays are tied. So you should use [{},{},{},{},{}] instead of 4 arrays. var items = [{pagenumber:1,zipcode:77889,streetname:Hillroad,roadnumber:55},{...},{...},{...},{...}] Then sort each key-value property one-by-one, like below:

var x= [ {a:2,b:2,c:3}, {a:1,b:1,c:1}, {a:1,b:2,c:3}, {a:2,b:2,c:2} ];

x.sort(function(item1, item2){ 
    var sort_a = item1.a-item2.a;
    if (sort_a) return sort_a;
    var sort_b = item1.b-item2.b;
    if (sort_b) return sort_b;
    var sort_c = item1.c-item2.c;
    if (sort_c) return sort_c;
})

Or simplify it to be

x.sort(function(item1, item2){ 
    return (item1.a-item2.a) || (item1.b-item2.b) || (item1.c-item2.c);
})
share|improve this answer
    
how does this help with sorting strings like the streetname? – Jaromanda X 4 mins ago

Given the data:

var pagenumber=[1,2,3,4,5]; //only numbers
var zipcode=[77889,99887,33667,11122,44559]; // only numbers
var streetname=['Hillroad', 'Hillroad', 'Baghdad Street', 'Hongway', 'Chinatown']; //only letters
var roadnumber=[55,27,1,13,16]; //only numbers

First, you need to make your data more easily manageable

var data = pagenumber.map(function(val, index) {
    return {
        pagenumber:val, 
        zipcode:zipcode[index],
        streetname:streetname[index],
        roadnumber:roadnumber[index]
    };
});

Then sort it

data.sort(function(a, b) {
     if (a.zipzode != b.zipcode) {
         // numeric
         return a.zipcode - b.zipcode;
     }
     if (a.streetname != b.streetname) {
         // alpha
         return a.streetname < b.streetname ? -1 : a.streetname > b.streetname ? 1 : 0;
     }
     if (a.roadnumber % 2 != b.roadnumber % 2) {
         // even before odd
         return b.roadnumber % 2 - a.roadnumber % 2;
     }
     // numeric
     return a.roadnumber - b.roadnumber;
});
share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.