Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The name mixes characters and numbers, which leads to a awkward sorting if you simply sort alphabetically.</p> <p>What you more seems to prefer is called <em>naturally sorting</em>, where the numbers within the text are really recognized as a number and the sort short apply their on a numeric basis.</p> <p>A few implementations of this algorithm can be found on <a href="http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html" rel="nofollow">Jeffs webpage</a>.</p> <p>The one i most prefer is the one <a href="http://www.davekoelle.com/files/AlphanumComparator.cs" rel="nofollow">from Dave</a>:</p> <pre><code>/* * The Alphanum Algorithm is an improved sorting algorithm for strings * containing numbers. Instead of sorting numbers in ASCII order like * a standard sort, this algorithm sorts numbers in numeric order. * * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com * * Based on the Java implementation of Dave Koelle's Alphanum algorithm. * Contributed by Jonathan Ruckwood &lt;jonathan.ruckwood@gmail.com&gt; * * Adapted by Dominik Hurnaus &lt;dominik.hurnaus@gmail.com&gt; to * - correctly sort words where one word starts with another word * - have slightly better performance * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ using System; using System.Collections; using System.Text; /* * Please compare against the latest Java version at http://www.DaveKoelle.com * to see the most recent modifications */ namespace AlphanumComparator { public class AlphanumComparator : IComparer { private enum ChunkType {Alphanumeric, Numeric}; private bool InChunk(char ch, char otherCh) { ChunkType type = ChunkType.Alphanumeric; if (char.IsDigit(otherCh)) { type = ChunkType.Numeric; } if ((type == ChunkType.Alphanumeric &amp;&amp; char.IsDigit(ch)) || (type == ChunkType.Numeric &amp;&amp; !char.IsDigit(ch))) { return false; } return true; } public int Compare(object x, object y) { String s1 = x as string; String s2 = y as string; if (s1 == null || s2 == null) { return 0; } int thisMarker = 0, thisNumericChunk = 0; int thatMarker = 0, thatNumericChunk = 0; while ((thisMarker &lt; s1.Length) || (thatMarker &lt; s2.Length)) { if (thisMarker &gt;= s1.Length) { return -1; } else if (thatMarker &gt;= s2.Length) { return 1; } char thisCh = s1[thisMarker]; char thatCh = s2[thatMarker]; StringBuilder thisChunk = new StringBuilder(); StringBuilder thatChunk = new StringBuilder(); while ((thisMarker &lt; s1.Length) &amp;&amp; (thisChunk.Length==0 ||InChunk(thisCh, thisChunk[0]))) { thisChunk.Append(thisCh); thisMarker++; if (thisMarker &lt; s1.Length) { thisCh = s1[thisMarker]; } } while ((thatMarker &lt; s2.Length) &amp;&amp; (thatChunk.Length==0 ||InChunk(thatCh, thatChunk[0]))) { thatChunk.Append(thatCh); thatMarker++; if (thatMarker &lt; s2.Length) { thatCh = s2[thatMarker]; } } int result = 0; // If both chunks contain numeric characters, sort them numerically if (char.IsDigit(thisChunk[0]) &amp;&amp; char.IsDigit(thatChunk[0])) { thisNumericChunk = Convert.ToInt32(thisChunk.ToString()); thatNumericChunk = Convert.ToInt32(thatChunk.ToString()); if (thisNumericChunk &lt; thatNumericChunk) { result = -1; } if (thisNumericChunk &gt; thatNumericChunk) { result = 1; } } else { result = thisChunk.ToString().CompareTo(thatChunk.ToString()); } if (result != 0) { return result; } } return 0; } } } </code></pre>
 

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