Skip to content

Commit f54109b

Browse files
committed
Simplify label comparison.
1 parent 4a099f6 commit f54109b

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

gentleSelect/jquery-gentleSelect.js

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -98,38 +98,27 @@
9898
isTruncated = false;
9999
}
100100

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-
111101
// 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();
129102

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;
133122
}
134123

135124
arr.sort(cmp);

0 commit comments

Comments
 (0)