Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To sort an array you use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort" rel="nofollow"><code>Array.sort</code></a> with an appropriate comparison function as an argument. The comparison function accepts two arguments, which in this case are expected to be objects with just a single property. You want to sort based on the name of that property.</p> <p>Getting an object's property names is most convenient with <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys" rel="nofollow"><code>Object.keys</code></a>, so we have this comparison function:</p> <pre><code>function(x, y) { var keyX = Object.keys(x)[0], keyY = Object.keys(y)[0]; if (keyX == keyY) return 0; return keyX &lt; keyY ? -1 : 1; } </code></pre> <p>It can be used like this:</p> <pre><code>var input = [{ d: "delete the text" }, { c: "copy the text" } ]; var sorted = input.sort(function(x, y) { var keyX = Object.keys(x)[0], keyY = Object.keys(y)[0]; if (keyX == keyY) return 0; return keyX &lt; keyY ? -1 : 1; }); </code></pre> <p><strong><a href="http://jsfiddle.net/cEFg3/" rel="nofollow">See it in action</a></strong>.</p> <p>Note that <code>Object.keys</code> requires a reasonably modern browser (in particular, IE version at least 9); otherwise you would need to write something such as this instead:</p> <pre><code>var keyX, keyY, name; for (name in x) { keyX = name; break; } for (name in y) { keyY = name; break; } </code></pre> <p><strong><a href="http://jsfiddle.net/fFRTD/" rel="nofollow">See it in action</a></strong>.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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