Touch the string numbers, sorted according to the proportion of the number of bits of each number are added to each, the final digital output string according to the proportion of the number of re-ascending sort

let str = '520 121 123 101 158 100 954';
                function sortNum(str) {
                    let arr = str.split(' ');
                    let finalList = [];
                    let result = '';
                    let jumpList = [];
                    arr.forEach((item)=>{
                        let startNum = 0;
                        item.split('').forEach((cur)=>{
                            startNum+= Number(cur);
                        });
                        jumpList.push(startNum);
                    });
                    arr.forEach((item,index)=>{
                        finalList.push({
                            a:item,
                            b:jumpList[index]
                        });
                    });
                    finalList.sort((a,b)=> a.b - b.b);
                    console.log(finalList);
                    finalList.forEach((item)=>{
                        result+=(item.a+' ');
                    });
                    console.log(result);
思路:
1 , an array of characters turn;
2, A string of small numbers of particles are added, the proportion of the composition of the array;
 3 , the proportion of the original string array and the array, placed in a new array, the specific gravity of the sorted array object, such objects sorted array;
 array 4, and the ordering traversal objects, remove the original string concatenation, i.e. a number of re-sorted;

 



  

Guess you like

Origin www.cnblogs.com/holy-amy/p/12195143.html