|
98 | 98 | isTruncated = false;
|
99 | 99 | }
|
100 | 100 |
|
101 |
| - // Determine if the list contains strings or numbers. |
102 |
| - var isAllNumeric = true; |
103 |
| - var startsWithNumberRegex = /^[0-9]+/; |
104 |
| - for (var i = 0; i < arr.length; i++) { |
105 |
| - if (!startsWithNumberRegex.test(arr[i])) { |
106 |
| - isAllNumeric = false; |
107 |
| - break; |
108 |
| - } |
109 |
| - } |
110 |
| - |
111 | 101 | // TODO: After you pick an option, close the dropdown, and animate the new item being added.
|
112 |
| - var cmp; |
113 |
| - if (isAllNumeric) { |
114 |
| - // Numeric comparison function. |
115 |
| - cmp = function(a, b) { |
116 |
| - var aMatch = startsWithNumberRegex.exec(a); |
117 |
| - var bMatch = startsWithNumberRegex.exec(b); |
118 |
| - |
119 |
| - if (aMatch != null) a = aMatch[0]; |
120 |
| - if (bMatch != null) b = bMatch[0]; |
121 |
| - |
122 |
| - return a - b; |
123 |
| - }; |
124 |
| - } else { |
125 |
| - // Compare by the order defined in the select element. |
126 |
| - cmp = function(a, b) { |
127 |
| - var aOption = all.find("option:contains('" + a + "')").first(); |
128 |
| - var bOption = all.find("option:contains('" + b + "')").first(); |
129 | 102 |
|
130 |
| - // TODO: Check for null? |
131 |
| - return aOption.attr("value") > bOption.attr("value"); |
132 |
| - } |
| 103 | + // Compare by the order defined in the select element. |
| 104 | + var cmp = function(lhs, rhs) { |
| 105 | + var valLhs; |
| 106 | + all.find('option').each(function(i) { |
| 107 | + if (this.text == lhs) { |
| 108 | + valLhs = Number(this.value); |
| 109 | + return false; |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + var valRhs; |
| 114 | + all.find('option').each(function(i) { |
| 115 | + if (this.text == rhs) { |
| 116 | + valRhs = Number(this.value); |
| 117 | + return false; |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + return valLhs > valRhs; |
133 | 122 | }
|
134 | 123 |
|
135 | 124 | arr.sort(cmp);
|
|
0 commit comments