Sort an array based on another array of integers

前端 未结 7 678
误落风尘
误落风尘 2020-11-27 21:59

Let\'s say I have an array: [0,3,4,2,5,1].

What I want to do is sort an array such as:

[\"one\", \"two\", \"three\", \"four\", \"five\",         


        
相关标签:
7条回答
  • 2020-11-27 23:01

    Not sur how you get your first array, but you could use an array of objects instead of [0,3,4,2,5,1]:

    var arr = [
      {n:0, s:'one'},
      {n:3, s:'four'},
      {n:4, s:'five'},
      {n:2, s:'three'},
      {n:5, s:'six'},
      {n:1, s:'two'}
    ]
    

    And avoid to process it.

    0 讨论(0)
提交回复
热议问题