Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript sort help
    text
    copied!<p>I'm trying to sort some xml into different arrays and I'm a bit stuck on how to go about this.</p> <p>Here is the xml: <a href="http://pastebin.ca/1754892" rel="nofollow noreferrer">http://pastebin.ca/1754892</a></p> <p>I'm using this xpath expression to get every "series" element that contains at least one "item" child node.</p> <pre> var ceerez = theXML.evaluate( '//series[item]' ,theXML, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null ); </pre> <p>then I'm iterating over the result and putting the elements in a regular array:</p> <pre><code>var newArr=[]; for (var m = 0; m &lt; ceerez.snapshotLength; m++){ newArr.push(ceerez.snapshotItem(m)); } </code></pre> <p>so what I would like to do next is to map and sort the elements in the newArr array into new arrays for each different sort. </p> <p>I want to have one array that is numerically sorted by the "pub" attribute (as numbers) for each series element.<br> One array that is sorted alphabetically for each "title" child element's text.<br> One array that is sorted by date for each "item" child element. </p> <p>I know how to do this with the sort() method, but I cant figure out how to do it from within the map() method so that I can make it into a new array. </p> <pre><code>var a2z1 = newArr.map(function(item){ item .sort(function(a,b) { return a.firstElementChild.textContent.toLowerCase().replace(/[^a-z,0-9]/gm, '') &lt; b.firstElementChild.textContent.toLowerCase().replace(/[^a-z,0-9]/gm, '') ? -1 : 1; }); ); </code></pre> <p>(a.firstElementChild) would be the "title" element.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload