尝试根据自定义字母对字符串数组进行排序。可能在那里有一些不必要的代码,但这是一些不同的迭代混合成一个。
我正在对第一个字母进行基本排序,如果这不起作用,我会调用深度排序函数并开始处理字母。但结果只按首字母排序,后者排序似乎是任意的。
任何帮助?
var wordArray = ['apple', 'abbot', 'aatrophy', 'banana', 'berry', 'cherrypie', 'cherry', 'candy', 'grapefruit', 'pear', 'pizza', 'zebra', 'cigarette', 'guitar'];
var wordToLetterArray = [];
// var sortingString = "kwfhjrsbdtqmxaopzvieulgcny";
var sortingString = "abcdefghijklmnopqrstuvwxyz";
var deepSort = function(wordArray1, wordArray2) {
var forLoopIterations = 0;
if (wordArray1 && wordArray2) {
if (wordArray1.length > wordArray2.length) {
forLoopIterations = wordArray2.length;
} else {
forLoopIterations = wordArray1.length;
}
for (var i = 0; i <= forLoopIterations; i++) {
if (sortingString.indexOf(wordArray1[i]) > sortingString.indexOf(wordArray2[i])) {
return -1;
} else if (sortingString.indexOf(wordArray1[i]) < sortingString.indexOf(wordArray2[i])) {
return 1
} else {
if (i >= forLoopIterations) {
if (wordArray1.length > wordArray2.length) {
return 1;
} else if (wordArray1.length < wordArray2.length) {
return -1
} else {
return 0
}
} else {
}
}
};
} else {
return 0;
}
}
var populateWordToLetterArray = function() {
for (var i = 0; i <= wordArray.length - 1; i++) {
wordToLetterArray.push([]);
for (var x = 0; x <= wordArray[i].length - 1; x++) {
wordToLetterArray[i].push(wordArray[i][x]);
};
};
sortWordArraybyFirstLetter();
}
var sortWordArraybyFirstLetter = function sortWordArraybyFirstLetter() {
wordArray.sort(function(a, b) {
var aIndex = sortingString.indexOf(a[0]);
var bIndex = sortingString.indexOf(b[0]);
if (aIndex > bIndex) {
return 1;
} else if (aIndex < bIndex) {
return -1;
} else {
return deepSort(wordToLetterArray[wordArray.indexOf(a)], wordToLetterArray[wordArray.indexOf(b)]);
}
})
}
populateWordToLetterArray();
console.log(wordArray);
console.log(wordToLetterArray);
&#13;
答案 0 :(得分:2)
创建一个功能,将单词“翻译”为您的自定义字母,然后通过比较他们的“翻译”对单词进行排序:
function translate(str, alphabet) {
var abc = "abcdefghijklmnopqrstuvwxyz";
return [].map.call(str, function(c) {
return alphabet[abc.indexOf(c)] || c;
}).join("");
}
var wordArray = ['apple', 'abbot', 'aatrophy', 'banana', 'berry', 'cherrypie', 'cherry', 'candy', 'grapefruit', 'pear', 'pizza', 'zebra', 'cigarette', 'guitar'];
var sortingString = "kwfhjrsbdtqmxaozpvieulgcny";
wordArray.sort(function(a, b) {
return translate(a, sortingString).localeCompare(translate(b, sortingString));
});
document.write(wordArray)
这不是特别有效,但仍有优化空间。
答案 1 :(得分:1)
当您深入嵌套时,很难对代码进行推理。你需要的是一种干净的方法来产生一个函数来根据你的排序顺序比较两个字符串。一旦你拥有了,一切都会变得更简单。
以下内容适用于此:
function makeComparer(order) {
var ap = Array.prototype;
// mapping from character -> precedence
var orderMap = {},
max = order.length + 2;
ap.forEach.call(order, function(char, idx) {
orderMap[char] = idx + 1;
});
function compareChars(l, r) {
var lOrder = orderMap[l] || max,
rOrder = orderMap[r] || max;
return lOrder - rOrder;
}
function compareStrings(l, r) {
var minLength = Math.min(l.length, r.length);
var result = ap.reduce.call(l.substring(0, minLength), function (prev, _, i) {
return prev || compareChars(l[i], r[i]);
}, 0);
return result || (l.length - r.length);
}
return compareStrings;
}
var comparer = makeComparer('abcdefghijklmnopqrstuvwxyz');
console.log(comparer('apple', 'abbot'));
console.log(comparer('abbot', 'apple'));
console.log(comparer('apple', 'apple'));
console.log(comparer('apple', 'apple pie'));
console.log(comparer('apple pie', 'apple'));
完成后,排序就像使用内置排序方法一样简单:
var comparer = makeComparer('abcdefghijklmnopqrstuvwxyz');
var wordArray = ['apple', 'abbot', 'aatrophy', 'banana',
'berry', 'cherrypie','cherry', 'candy',
'grapefruit', 'pear', 'pizza', 'zebra',
'cigarette', 'guitar'];
wordArray.sort(comparer);
完整解决方案:
function makeComparer(order) {
var ap = Array.prototype;
// mapping from character -> precedence
var orderMap = {},
max = order.length + 2;
ap.forEach.call(order, function(char, idx) {
orderMap[char] = idx + 1;
});
function compareChars(l, r) {
var lOrder = orderMap[l] || max,
rOrder = orderMap[r] || max;
return lOrder - rOrder;
}
function compareStrings(l, r) {
var minLength = Math.min(l.length, r.length);
var result = ap.reduce.call(l.substring(0, minLength), function (prev, _, i) {
return prev || compareChars(l[i], r[i]);
}, 0);
return result || (l.length - r.length);
}
return compareStrings;
}
var wordArray = ['apple', 'abbot', 'aatrophy', 'banana',
'berry', 'cherrypie','cherry', 'candy',
'grapefruit', 'pear', 'pizza', 'zebra',
'cigarette', 'guitar'];
var comparer = makeComparer('abcdefghijklmnopqrstuvwxyz');
console.log(wordArray.slice().sort(comparer));
var weirdComparer = makeComparer("kwfhjrsbdtqmxaopzvieulgcny");
console.log(wordArray.slice().sort(weirdComparer));