Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two solutions, these being one to use javascript and one to use PHP.</p> <h2><strong>Pure Javascript Solution:</strong></h2> <p>Here is how to sort your <code>&lt;li&gt;</code> based on the data-date attribute. </p> <p>I've included both date based and string based sorting. </p> <p>JSFiddle: <a href="http://jsfiddle.net/xZaeu/" rel="nofollow noreferrer">http://jsfiddle.net/xZaeu/</a></p> <pre><code>&lt;html&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"&gt;&lt;/script&gt; &lt;body&gt; &lt;ul id="ulList"&gt; &lt;li&gt;&lt;a data-date="2013-09-16 11:44:50" href="http://example.com/url/"&gt;09!&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-date="2013-08-16 11:44:50" href="http://example.com/url/"&gt;08!&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-date="2013-07-16 11:44:50" href="http://example.com/url/"&gt;07!&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-date="2013-11-16 11:44:50" href="http://example.com/url/"&gt;11!&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-date="2013-10-16 11:44:50" href="http://example.com/url/"&gt;10!&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-date="2013-06-16 11:44:50" href="http://example.com/url/"&gt;06!&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;script&gt; function compareDataDates(a,b) { var match = a.match(/^(\d+)-(\d+)-(\d+) (\d+)\:(\d+)\:(\d+)$/); var date1 = new Date(match[1], match[2] - 1, match[3], match[4], match[5], match[6]); match = b.match(/^(\d+)-(\d+)-(\d+) (\d+)\:(\d+)\:(\d+)$/); var date2 = new Date(match[1], match[2] - 1, match[3], match[4], match[5], match[6]) return date1 - date2; } var list = $('#ulList'); var listItems = list.find('li').sort(function (a, b) { return compareDataDates($(a).find('a').eq(0).attr("data-date"), $(b).find('a').eq(0).attr("data-date")); //return $(a).find('a').eq(0).attr("data-date").localeCompare($(b).find('a').eq(0).attr("data-date")); }); list.find('li').remove(); list.append(listItems); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Both String and date comparison work since your using the specific format, though the string based comparison would fail in a situation where you have:</p> <pre><code>&lt;li&gt;&lt;a data-date="2013-06-16 11:44:50" href="http://example.com/url/"&gt;06!&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-date="2013-6-16 11:44:50" href="http://example.com/url/"&gt;6!&lt;/a&gt;&lt;/li&gt; </code></pre> <p><strong>References used cooking this up:</strong></p> <ul> <li><a href="https://stackoverflow.com/questions/6076820/how-to-order-dynamically-created-elements-based-on-a-custom-attribute">How to order dynamically created elements based on a custom attribute?</a></li> <li><a href="https://stackoverflow.com/questions/471606/get-all-hrefs-as-an-array-in-jquery">Get all hrefs as an array in jQuery</a></li> <li><a href="https://stackoverflow.com/questions/2167602/optimum-way-to-compare-strings-in-javascript">Optimum way to compare strings in Javascript?</a></li> <li><a href="https://stackoverflow.com/questions/11170054/compare-two-dates-with-javascript">Compare dates with javascript</a></li> <li><a href="https://stackoverflow.com/questions/6704325/how-to-convert-date-in-format-yyyy-mm-dd-hhmmss-to-unix-timestamp">How to convert date in format &quot;YYYY-MM-DD hh:mm:ss&quot; to UNIX timestamp</a></li> <li><a href="https://stackoverflow.com/questions/2173971/jquery-find-to-get-the-first-element">jquery find to get the first element</a></li> </ul> <h2><strong>Pure PHP Solution:</strong></h2> <p>Something along the lines of...</p> <pre><code> &lt;?php //Other PHP code here... $arrayForSorting = new array(); foreach ($notifications as &amp;$value) { $temp = addslashes($value); array_push($arrayForSorting,"&lt;li&gt;".$temp."&lt;/li&gt;"); } //Sort the array asort($arrayForSorting); foreach ($arrayForSorting as &amp;$value) { echo $value; } ?&gt; </code></pre> <p><strong>References:</strong></p> <ul> <li><a href="http://php.net/manual/en/array.sorting.php" rel="nofollow noreferrer">http://php.net/manual/en/array.sorting.php</a></li> </ul>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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