我想知道除了手动排序以下内容(示例)之外,还有其他方法吗?
BBB+
BB
AA+
A-
BB-
A
B
AAA
我们正在尝试将先前的字符串排序如下:
AAA
AA+
A
A-
BBB+
BB
BB-
B
是否有任何自动排序将加号和减号放在首位进行比较?
答案 0 :(得分:3)
正如我在评论中所描述的,我们可以将等级更改为数字,这样我们就可以对复杂的字母排序使用基本排序。
const ratings = [
"BBB+",
"BB",
"AA+",
"A-",
"BB-",
"A",
"B",
"AAA"
];
// Each letter should be enough apart.
// Technically we can go for smaller numbers.
// But that can break if not carefull once we start using Moodys or S&P instead of Fitch.
// Since AA- is lower than AA, the - character has to be a negative number.
const weights = {
"A": 10000,
"B": 1000,
"C": 100,
"D": 10,
"+": 1,
"-": -1
};
const rating_values = ratings
.map( rating => ( {
rating,
"value": rating
.split( '' )
.map( character => weights[ character ] )
.reduce( ( a, b ) => a + b )
} ) );
const sorted_ratings = rating_values
.sort( ( a, b ) => b.value - a.value )
.map( obj => obj.rating );
console.log( sorted_ratings );
答案 1 :(得分:1)
您可能想执行以下操作:
let marks = {"A": 6, "B": 1, "+": 0.5, "-": -0.5}
customGradeSort = (mark1, mark2) => {
let value1 = mark1.split("").reduce((total, e) => {
total += marks[e];
return total;
}, 0);
let value2 = mark2.split("").reduce((total, e) => {
total += marks[e];
return total;
}, 0);
return (value2 - value1)
}
const myMarks = ["BBB+", "BB","AA+","A-","BB-","A","B","AAA"]
console.log(myMarks.sort(customGradeSort));
注意:取A
的值,使A-
> BBB+
在这种情况下,A-
是5.5,BBB+
是3.5。
答案 2 :(得分:1)
我通过确定排序标准来解决这个问题。在这种情况下,它们的顺序如下:alphabetical
,length
和symbol
。
这使我想到了一个简单而简短的解决方案。
它还涵盖了字母和数量不同的案件:
const input = ["BBB+", "BB", "AA+", "A-", "BB-","A","B", "AAA"];
const sorted = input.sort((a,b) => {
const [ [ aStr, aSign ], [ bStr, bSign ] ] = [a,b].map(str => str.match(/[A-Z]+|[\+|\-]/gi));
const [aSignNum, bSignNum] = [aSign, bSign].map(a => a === "+" ? 1 : a === "-" ? -1 : 0);
const alphCriteria = aStr[0].localeCompare(bStr[0]);
const countCriteria = bStr.length - aStr.length;
const signCriteria = bSignNum - aSignNum;
return alphCriteria || countCriteria || signCriteria;
});
console.log(sorted);
答案 3 :(得分:1)
您可以通过将加号/减号中的字母分开来进行三步排序。
为了获得'+'
和'-'
的可排序值,此方法对这些字符使用权重,对于未给定的符号取零。
然后
'+'
小于'-'
)
var data = ['BBB+', 'BB', 'AA+', 'A-', 'BB-', 'A', 'B', 'AAA', 'AAA+', 'AAA-'];
data.sort((a, b) => {
var weight = { '+': -1, '-': 1 },
aa = a.split(/(?=[+\-])/),
bb = b.split(/(?=[+\-])/);
return aa[0][0].localeCompare(bb[0][0])
|| bb[0].localeCompare(aa[0])
|| (weight[aa[1]] || 0) - (weight[bb[1]] || 0);
});
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 4 :(得分:0)
知道我迟到了,我仍然会贡献力量。
我做了什么:
let arr = ["A", "A-", "AA+", "AAA", "B", "BB", "BB-", "BBB+"];
let weights = {
A: 100,
B: 10,
'+': 1,
'-': -1
}
function getWeightSum(str) {
let weightSum = 0;
// Iterate all characters in string
// Accumulate total weight
for (let i = 0; i < str.length; i++) {
weightSum += weights[str[i]];
}
return weightSum;
}
function stringSort(a, b) {
let aInt = getWeightSum(a);
let bInt = getWeightSum(b);
if (aInt > bInt) return -1;
else if (aInt <= bInt) return 1;
else return 0;
}
console.log(arr.sort(stringSort));
答案 5 :(得分:0)
我的版本与Nina的版本相似(我通过窃取答案的localeCompare
进行了简化,而不是我的第一个相当愚蠢的嵌套条件运算符。)但是,它捕获了每个元素的一些不同描述,并使用了分解和命名值而不是数组索引。
它处理+
/ -
的方式也略有不同。
const compareRatings = (a, b) => {
// e.g. ['BBB', 'B', '+'] or ['BB', 'B', '']
const describe = r => r.match(/(([A-Z])\2*)([\-\+]?)/).slice(1)
const [base1, char1, ext1] = describe(a)
const [base2, char2, ext2] = describe(b)
return char1.localeCompare(char2) ||
base2.localeCompare(base1) ||
// Unicode: 43: '+', 44: ',', 45: '-'
((ext1 || ',').charCodeAt(0) - (ext2 || ',').charCodeAt(0))
}
const ratings = ['BBB+', 'BB', 'AA+', 'A-', 'BB-', 'A', 'B', 'AAA']
console.log(ratings.sort(compareRatings))
相对于那些对字母加权的方法,我更喜欢这种技术,因为这种方式可以扩展到您喜欢的任意数量的字母以及每个字母的副本。例如,它将不会像其中许多那样在“ A”之前对“ BBBBBBBBBBB”进行排序,并且可以很好地处理“ XX-”。