-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargesrNumber.js
50 lines (44 loc) · 1.17 KB
/
largesrNumber.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//example 1
const LargestNumbers = (nums) => {
nums.sort((a, b) => `${b}${a} - ${a}${b}`);
return nums[0] === 0 ? "" + nums[0] : nums.join("");
};
//example 2
function LargestNumber(nums) {
const strNums = [];
for (let i = 0; i < nums.length; i++) {
strNums.push(nums[i].toString());
}
const compareFunc = (a, b) => {
const order1 = a + b;
const order2 = b + a;
if (order1 > order2) {
return -1; // a comes before b
} else if (order1 < order2) {
return 1; // b comes before a
} else {
return 0; // order doesn't matter
}
};
// Sort the numbers using the custom sorting function
for (let i = 0; i < strNums.length; i++) {
for (let j = i + 1; j < strNums.length; j++) {
if (compareFunc(strNums[i], strNums[j]) === 1) {
// Swap the two strings
const temp = strNums[i];
strNums[i] = strNums[j];
strNums[j] = temp;
//[[strNums[i],strNums[j]] = [strNums[j],strNums[i]]]
}
}
}
let result = "";
for (let i = 0; i < strNums.length; i++) {
result += strNums[i];
}
if (result[0] === "0") {
return "0";
}
return result;
}
console.log(LargestNumber([]))