4

## 如何解決 JavasSript [7,-2,2,-7,-3].sort() = [-2,-3,-7,2,-7] 問題

``````[7,-2,2,-7,-3].sort() = [-2,-3,-7,2,-7]
``````

(a, b) => a.toString().localeCompare(b)

``````var arr = [7,-2,2,-7,-3];
arr.sort(function (a,b) {
if (a < b ) return -1;
if (a > b ) return 1;
return 0;
});
``````

``````var arr = [7,-2,2,-7,-3];
arr.sort((x, y) => x - y);
``````

`先map再sort``先sort再map`結果可能會不一樣問題

``````//異常排序
var data = [
{value: 4},
{value: 2},
{value: undefined},
{value: undefined},
{value: 1},
{value: undefined},
{value: undefined},
{value: 7},
{value: undefined},
{value: 4}
];

data
.sort((x, y) => x.value - y.value)
.map(x => x.value);
// result : (10) [2, 4, undefined, undefined, 1, undefined, undefined, 4, 7, undefined]

//能正常排序
var data = [
{value: 4},
{value: 2},
{value: undefined},
{value: undefined},
{value: 1},
{value: undefined},
{value: undefined},
{value: 7},
{value: undefined},
{value: 4}
];

data
.map(x => x.value)
.sort((x, y) => x.value - y.value);
// result : [4, 2, 1, 7, 4, undefined, undefined, undefined, undefined, undefined]
``````

listennn08 :
lodash
_.sortBy(data, (el) => el.value)

``````<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
<script>
//能正常排序
var data = [
{value: 4},
{value: 2},
{value: undefined},
{value: undefined},
{value: 1},
{value: undefined},
{value: undefined},
{value: 7},
{value: undefined},
{value: 4}
];

var sorted =
_.map(
_.sortBy(data, (el) => el.value)
,(el) => el.value
)
;

console.log(sorted);//[1, 2, 4, 4, 7, undefined, undefined, undefined, undefined, undefined]
</script>
``````

kekeke iT邦新手 4 級 ‧ 2020-09-14 15:27:39 檢舉
sort 中可以擺想要的順序~
hokou iT邦新手 4 級 ‧ 2020-09-14 15:30:25 檢舉
[1,2,5,10].sort((a, b) => a - b)

「為何1會比10大」，這就是排序第一個要面臨的問題。

```
[1,10,3].sort();
(3) [1, 10, 3]
```
lodash
_.sortBy(data, (el) => el.value)

1 在 10 前面 3 在 10 後面 有發現了嗎?

### 2 個回答

3
wrxue
iT邦研究生 4 級 ‧ 2020-09-14 15:27:33

``````Array.prototype.mysort = function() {
return this.sort(function(a, b) {
return a - b;
})
}

[7,-2,2,-7,-3, 27].mysort()
output -> [-7, -3, -2, 2, 7, 27]
``````

`先map再sort跟先sort再map結果可能會不一樣`

``````//異常排序
var data = [
{value: 4},
{value: 2},
{value: undefined},
{value: undefined},
{value: 1},
{value: undefined},
{value: undefined},
{value: 7},
{value: undefined},
{value: 4}
];

data
.sort((x, y) => x.value - y.value)
.map(x => x.value);
// result : (10) [2, 4, undefined, undefined, 1, undefined, undefined, 4, 7, undefined]

//能正常排序
var data = [
{value: 4},
{value: 2},
{value: undefined},
{value: undefined},
{value: 1},
{value: undefined},
{value: undefined},
{value: 7},
{value: undefined},
{value: 4}
];

data
.map(x => x.value)
.sort((x, y) => x.value - y.value);
// result : [4, 2, 1, 7, 4, undefined, undefined, undefined, undefined, undefined]
``````
4

iT邦研究生 1 級 ‧ 2020-09-14 17:08:14

``````var data = [
{value: 4},
{value: 2},
{value: undefined},
{value: undefined},
{value: 1},
{value: undefined},
{value: undefined},
{value: 7},
{value: undefined},
{value: 4}
];

data.sort((a, b)=>{
var type_a = typeof a.value ==='number' ? 0 : 1;
var type_b = typeof b.value ==='number' ? 0 : 1;
if(type_a|type_b) {
return type_a-type_b;
}
return a.value-b.value;
});
``````

``````function stableSort(arr, cmpFunc) {
let wrapArray=arr.map((v,i)=>{
return {idx:i, val:v}
});
wrapArray.sort((a, b)=>{
var cmp=cmpFunc(a.val, b.val);
return cmp===0?a.idx-b.idx:cmp;
});
wrapArray.forEach((x,i)=>{
arr[i]=x.val;
});
return arr;
}
``````