Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按索引对字符串数组排序-JavaScript_Javascript_Arrays_Sorting - Fatal编程技术网

按索引对字符串数组排序-JavaScript

按索引对字符串数组排序-JavaScript,javascript,arrays,sorting,Javascript,Arrays,Sorting,如何按索引升序或降序对字符串数组排序: var arr = ['heck', 'yeah', 'soon', 'greet']; return arr.slice(0, 10).sort((a, b) => b > a).map(function(tag){ console.log(tag); }); 我不能在这里使用长度,因为默认情况下,它将根据长度对其进行排序 有什么想法吗 谢谢 sort()方法对数组中的元素进行就地排序。默认排序顺序根据字符串Unicode代码点。所

如何按索引升序或降序对字符串数组排序:

var arr = ['heck', 'yeah', 'soon', 'greet'];

return arr.slice(0, 10).sort((a, b) => b > a).map(function(tag){
   console.log(tag);
});
我不能在这里使用长度,因为默认情况下,它将根据长度对其进行排序

有什么想法吗

谢谢

sort()方法对数组中的元素进行就地排序。默认排序顺序根据字符串Unicode代码点。所以你可以用

acs:

描述:

sort()方法对数组中的元素进行就地排序。默认排序顺序根据字符串Unicode代码点。所以你可以用

acs:

描述:


请添加想要的结果。你指的是哪个索引?asc:
arr.sort()
desc:
arr.sort();反向排列()@headmaxL知道了。所谓“按索引排序”是指“按字母顺序排序”?请添加想要的结果。你指的是哪个索引?asc:
arr.sort()
desc:
arr.sort();反向排列()@headmaxL明白了。“按索引排序”是指“按字母顺序排序”?
arr.sort()
arr.sort(); 
arr.reverse();