Sort two arrays the same way

前端 未结 12 2268
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 15:48

For example, if I have these arrays:

var name = [\"Bob\",\"Tom\",\"Larry\"];
var age =  [\"10\", \"20\", \"30\"];

And I use name.sort

12条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 16:22

    Simplest explantion is the best, merge the arrays, and then extract after sorting: create an array

    name_age=["bob@10","Tom@20","Larry@30"];
    

    sort the array as before, then extract the name and the age, you can use @ to reconise where name ends and age begins. Maybe not a method for the purist, but I have the same issue and this my approach.

提交回复
热议问题