fix Ox.sort so that it handles multiple numbers

This commit is contained in:
rlx 2012-04-24 10:47:18 +00:00
parent b4fd0f01ae
commit 959a014657

View file

@ -411,22 +411,21 @@ Ox.range = function() {
function getSortValues(arr, fn) {
var arr_ = fn ? arr.map(fn) : arr,
len, matches = {}, sort = {};
len, matches = [], sort = {};
// find numbers
arr.forEach(function(val, i) {
var match;
if (Ox.isString(val)) {
match = /\d+/.exec(arr_[i]);
matches[val] = match ? match[0] : '';
match = arr_[i].match(/\d+/g);
match && Ox.merge(matches, match);
}
});
// get length of longest number
len = Ox.max(Ox.map(matches, function(val) {
return val.length;
}));
// pad numbers, make lowercase,
// remove leading non-word characters
// and move leading articles to the end
// make lowercase, remove leading non-word characters,
// pad numbers and move leading articles to the end
arr.forEach(function(val, i) {
var val_ = arr_[i];
if (
@ -436,16 +435,14 @@ Ox.range = function() {
) {
sort[val] = null;
} else if (Ox.isString(val_)) {
sort[val] = (
matches[val]
? val_.replace(
matches[val], Ox.pad(matches[val], len)
)
: val_
).toLowerCase().replace(/^\W+/, '');
sort[val] = val_.toLowerCase()
.replace(/^\W+/, '')
.replace(/\d+/g, function(match) {
return Ox.pad(match, len);
});
Ox.forEach(['a', 'an', 'the'], function(article) {
var len;
if (new RegExp('^' + article + ' ', 'i').test(sort[val])) {
if (new RegExp('^' + article + ' ').test(sort[val])) {
len = article.length;
sort[val] = sort[val].substr(len + 1) + ', '
+ sort[val].substr(0, len);