Note that there are some explanatory texts on larger screens.

plurals
  1. POordering array of objects using JavaScript
    primarykey
    data
    text
    <p>I have the following array of objects which orders the list. The problem is that the ordering is wrong because the OrderId property is not unique across all headings. The OrderId starts from 1 for each heading, hence the problem. Please help! Many thanks</p> <pre><code>// Class var Item = function(orderId, forename, surname, heading) { this.OrderId = orderId; this.Forename = forename; this.Surname = surname; this.Heading = heading; }; // Creation of objects var obj1 = new Item(1, "James", "Smith", "Heading1"); var obj2 = new Item(2, "Tracey", "Janes", "heading1"); var obj3 = new Item(3, "Sarah", "Cann", "Heading1"); var obj4 = new Item(1, "Matt", "Bars", "Heading2"); var obj4 = new Item(2, "Alex", "Magi", "Heading2"); // Add to array tempArray.push(obj1); tempArray.push(obj2); tempArray.push(obj3); tempArray.push(obj4); // Sort array tempArray.sort(function(a, b) { var a1 = a.OrderId, b1 = b.OrderId; if (a1 == b1) return 0; return a1 &gt; b1 ? 1 : -1; }); // Render array to screen - order by OrderId for(var i = 0; i &lt; tempArray.length; i++) { console.log(tempArray[i].Heading); console.log(tempArray[i].Forename + " " + tempArray[i].Surname); } </code></pre> <p><strong>The output I need:</strong></p> <pre><code>Heading 1 James Smith Tracey Janes Sarah Cann Heading 2 Matt Bars Alex Magi </code></pre> <p>Because the OrderId is not unique across I get the following issue</p> <pre><code>Heading 1 James Smith Matt Bars Alex Magi Tracey Janes Sarah Cann Heading 2 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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