|
| 1 | +// This code is from https://github.com/ftognetto/alphanum_comparator/ |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you |
| 4 | +// may not use this file except in compliance with the License. You may |
| 5 | +// obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | +// implied. See the License for the specific language governing permissions |
| 13 | +// and limitations under the License. |
| 14 | + |
| 15 | +class AlphanumComparator { |
| 16 | + static bool _isDigit(String ch) { |
| 17 | + return (ch.codeUnitAt(0) >= 48) && (ch.codeUnitAt(0) <= 57); |
| 18 | + } |
| 19 | + |
| 20 | + /// Length of string is passed in for improved efficiency (only need to calculate it once) |
| 21 | + static String _getChunk(String s, int slength, int marker) { |
| 22 | + final StringBuffer chunk = StringBuffer(); |
| 23 | + int c = s.codeUnitAt(marker); |
| 24 | + chunk.write(c); |
| 25 | + marker++; |
| 26 | + if (_isDigit(s)) { |
| 27 | + while (marker < slength) { |
| 28 | + c = s.codeUnitAt(marker); |
| 29 | + if (!_isDigit(s)) break; |
| 30 | + chunk.write(c); |
| 31 | + marker++; |
| 32 | + } |
| 33 | + } else { |
| 34 | + while (marker < slength) { |
| 35 | + c = s.codeUnitAt(marker); |
| 36 | + if (_isDigit(s)) break; |
| 37 | + chunk.write(c); |
| 38 | + marker++; |
| 39 | + } |
| 40 | + } |
| 41 | + return chunk.toString(); |
| 42 | + } |
| 43 | + |
| 44 | + /// Compare using alphanum algorithm |
| 45 | + static int compare(String s1, String s2) { |
| 46 | + int thisMarker = 0; |
| 47 | + int thatMarker = 0; |
| 48 | + final int s1Length = s1.length; |
| 49 | + final int s2Length = s2.length; |
| 50 | + |
| 51 | + while (thisMarker < s1Length && thatMarker < s2Length) { |
| 52 | + final String thisChunk = _getChunk(s1, s1Length, thisMarker); |
| 53 | + thisMarker += thisChunk.length; |
| 54 | + |
| 55 | + final String thatChunk = _getChunk(s2, s2Length, thatMarker); |
| 56 | + thatMarker += thatChunk.length; |
| 57 | + |
| 58 | + // If both chunks contain numeric characters, sort them numerically |
| 59 | + int result = 0; |
| 60 | + if (_isDigit(thisChunk) && _isDigit(thatChunk)) { |
| 61 | + // Simple chunk comparison by length. |
| 62 | + final int thisChunkLength = thisChunk.length; |
| 63 | + result = thisChunkLength - thatChunk.length; |
| 64 | + // If equal, the first different number counts |
| 65 | + if (result == 0) { |
| 66 | + for (int i = 0; i < thisChunkLength; i++) { |
| 67 | + result = thisChunk.codeUnitAt(i) - thatChunk.codeUnitAt(i); |
| 68 | + if (result != 0) { |
| 69 | + return result; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } else { |
| 74 | + result = thisChunk.compareTo(thatChunk); |
| 75 | + } |
| 76 | + |
| 77 | + if (result != 0) return result; |
| 78 | + } |
| 79 | + |
| 80 | + return s1Length - s2Length; |
| 81 | + } |
| 82 | + |
| 83 | + /// Compare using alphanum algorithm, but sort strings starting with a number firsts |
| 84 | + static int compareNumFirsts(String s1, String s2) { |
| 85 | + RegExp numberRegEx = RegExp(r'[0-9]'); |
| 86 | + bool isS1startWithNumber = s1.startsWith(numberRegEx); |
| 87 | + bool isS2startWithNumber = s2.startsWith(numberRegEx); |
| 88 | + |
| 89 | + if (isS1startWithNumber && !isS2startWithNumber) { |
| 90 | + return -1; |
| 91 | + } else if (isS2startWithNumber && !isS1startWithNumber) { |
| 92 | + return 1; |
| 93 | + } |
| 94 | + return compare(s1, s2); |
| 95 | + } |
| 96 | +} |
0 commit comments