如何为javascript排序函数制作一个有效的比较器来对字符串数组进行排序?
我有一个字符串数组,看起来像:如何为javascript排序函数制作一个有效的比较器来对字符串数组进行排序?,javascript,string,sorting,comparator,Javascript,String,Sorting,Comparator,我有一个字符串数组,看起来像: array = ['third', 'first', 'fourth', 'second', 'custom2', 'custom1'] array = ['first', 'second', 'third', 'fourth', 'custom2', 'custom1'] 我想对这个数组进行排序,它看起来像: array = ['third', 'first', 'fourth', 'second', 'custom2', 'custom1'] array
array = ['third', 'first', 'fourth', 'second', 'custom2', 'custom1']
array = ['first', 'second', 'third', 'fourth', 'custom2', 'custom1']
我想对这个数组进行排序,它看起来像:
array = ['third', 'first', 'fourth', 'second', 'custom2', 'custom1']
array = ['first', 'second', 'third', 'fourth', 'custom2', 'custom1']
特定字符串,如“first”、“second”。。。应按给定顺序排序(第一个在第二个在第三个之前…),任何其他字符串应按任意顺序附加在末尾。仅包含这些字符串子集的数组应按正确顺序排序:
['fourth', 'something', 'second'] => ['second', 'fourth', 'something']
我想知道是否有可能为javascript sort()函数编写一个比较器函数来有效地解决这个问题。类似的东西
array = ['third', 'first', 'fourth', 'second', 'custom2', 'custom1']
special = ['first', 'second', 'third', 'fourth']
array.sort(function(a, b) {
var ia = special.indexOf(a)
var ib = special.indexOf(b)
if(ia >= 0 && ib >= 0) return ia - ib;
if(ia >= 0) return -1;
if(ib >= 0) return +1;
return a > b ? 1 : a == b ? 0 : -1;
})
console.log(array)
[
"first",
"second",
"third",
"fourth",
"custom1",
"custom2"
]
或者,最好使用:
a=array.map(函数(x){
var n=特殊指数x(x);
返回[n<0?数组。长度:n,x]
}).排序(功能(a、b){
返回(a[0]-b[0])| |(a[1]>b[1]-1:a[1]==b[1]-0:-1);
}).map(函数(x){
返回x[1]
})
像这样的东西
array = ['third', 'first', 'fourth', 'second', 'custom2', 'custom1']
special = ['first', 'second', 'third', 'fourth']
array.sort(function(a, b) {
var ia = special.indexOf(a)
var ib = special.indexOf(b)
if(ia >= 0 && ib >= 0) return ia - ib;
if(ia >= 0) return -1;
if(ib >= 0) return +1;
return a > b ? 1 : a == b ? 0 : -1;
})
console.log(array)
[
"first",
"second",
"third",
"fourth",
"custom1",
"custom2"
]
或者,最好使用:
a=array.map(函数(x){
var n=特殊指数x(x);
返回[n<0?数组。长度:n,x]
}).排序(功能(a、b){
返回(a[0]-b[0])| |(a[1]>b[1]-1:a[1]==b[1]-0:-1);
}).map(函数(x){
返回x[1]
})
到目前为止,您能否通过现场演示发布您的代码,并解释哪些代码不起作用?嗯。。。我还没有真正的工作代码,我有这个字符串数组,用作键,我想对它们进行排序。因此,我搜索了一种可能性,找到了javascript sort()函数的comparator函数参数。我坚持了。你能用一个现场演示发布你的代码并解释什么不起作用吗?嗯。。。我还没有真正的工作代码,我有这个字符串数组,用作键,我想对它们进行排序。因此,我搜索了一种可能性,找到了javascript sort()函数的comparator函数参数。非常感谢,这个特殊的数组是一个好主意,非常好的代码,谢谢!如果可以的话,我会给schwartzian变换另一个+1,因为你把它带回到我的脑海中,我可以在今天晚些时候完美地使用它:是否有可能改变你的例子,即数组包含一些ID,并且应该按相关值排序?像数组=['ID3'、'ID1'、'ID4'、'ID2'、'IDxz'、'IDxy']对={'ID3':'third'、'ID1':'first'、'ID4':'first'、'ID2':'second'、'IDxz':'custom2'、'IDxy':'custom1'}?我可以通过用pairs[a]/pairs[b]替换a/b来改变你的比较器函数,但这对schwartzian变换不起作用。@karlitos:只需将n=special.indexOf(x)
替换为n=pairs[x]| array.length
。非常感谢,这个特殊数组是一个好主意,非常好的代码,谢谢!如果可以的话,我会给schwartzian变换另一个+1,因为你把它带回到我的脑海中,我可以在今天晚些时候完美地使用它:是否有可能改变你的例子,即数组包含一些ID,并且应该按相关值排序?像数组=['ID3'、'ID1'、'ID4'、'ID2'、'IDxz'、'IDxy']对={'ID3':'third'、'ID1':'first'、'ID4':'first'、'ID2':'second'、'IDxz':'custom2'、'IDxy':'custom1'}?我可以通过用pairs[a]/pairs[b]替换a/b来更改您的比较器函数,但这对schwartzian变换不起作用。@karlitos:只需将n=special.indexOf(x)
替换为n=pairs[x]| array.length
。