# python 数组如何排序

xxxxx
• 3
``````a1 = [2, 1, 3]
b1 = [1, 1, -2]

a1是数据，b1是下标，请问如何根据b1提供的下标对a1进行排序，其中b2里面的-2代表向左边移动2位，所以最后的结果是[3,2,1]，请问大佬应该如何实现！！

##### 3个回答
✓ 已被采纳

Python 版

``````ret = [0] * len(a1)
for (x, y), i in zip(enumerate(a1), b1):
ret[x + i] = y
print(ret)``````

typescript版本

``````const a1 = [2, 1, 3];
const b1 = [1, 1, -2];

const sort = (arr: number[], indexes: number[]): number[] => {
const temp: number[] = Array.from({ length: arr.length });

for (let i = 0; i < arr.length; ++i) {
temp[i + indexes[i]] = arr[i];
}

return temp;
};

console.log(sort(a1, b1));
``````
``````def offset_execute(arr,off):
do_offset(arr,off,0,arr[0],0)

def do_offset(arr,offset,start,item,count):
off = offset[start]
position = start + off
nextItem = arr[position]
arr[position] = item
count = count + 1
if(count != len(arr)):
do_offset(arr,offset,position,nextItem,count)

arr = [2,1,3]

offset = [1,1,-2]

offset_execute(arr,offset)

print(arr)

``````

##### 撰写回答
###### 你尚未登录，登录后可以
• 和开发者交流问题的细节
• 关注并接收问题和回答的更新提醒
• 参与内容的编辑和改进，让解决方法与时俱进