Note that there are some explanatory texts on larger screens.

plurals
  1. POSort javascript array so that blank values are always at bottom
    primarykey
    data
    text
    <p>So I have an array of arrays which contain only strings. The array of arrays is to be displayed as a table and may have over 1000 rows with 20 or more values in each.</p> <p>eg:</p> <pre><code>var arr = [ ["bob","12","yes"], ["joe","","no"], ["tim","19","no"], ["dan","","yes"], ["tim","",""], ["dan","0",""] ] </code></pre> <p>the strings may contain anything that can be represented as a string, including: <code>" "</code>, <code>""</code>, <code>"0"</code> or <code>"00-00-00"</code> etc... and any column my be used for ordering.</p> <p>I am sorting the arrays ascending and descending but some of the values I am sorting by are blank strings: <code>""</code>. How could I get the blank strings (only) to always be at the end of the new arrays in all modern browsers?</p> <p>currently they are at the end when ascending but at the start when descending.</p> <p>I am sorting like below (Yes I'm sure I can do it shorter too):</p> <pre><code>if (direction == "asc") { SortedArr = arr.sort(function (a, b) { if (a[colToSortBy] == '') { return -1; } if (a[colToSortBy].toUpperCase() &lt; b[colToSortBy].toUpperCase()) { return -1; } if (a[colToSortBy].toUpperCase() &gt; b[colToSortBy].toUpperCase()) { return 1; } return 0; }); } else { SortedArr = arr.sort(function (a, b) { if (a[colToSortBy] == '') { return -1; } if (b[colToSortBy].toUpperCase() &lt; a[colToSortBy].toUpperCase()) { return -1; } if (b[colToSortBy].toUpperCase() &gt; a[colToSortBy].toUpperCase()) { return 1; } return 0; }); } </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.
    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