Javascript 根据Node.js中的多个条件对数组排序

Javascript 根据Node.js中的多个条件对数组排序,javascript,node.js,sorting,Javascript,Node.js,Sorting,我很难正确地排序一组对象。这是排序条件的预期结果和解释: Test data: POST /pet​/{petId}​/uploadImage POST /pet PUT /pet GET /pet​/findByStatus GET /pet​/{petId} POST /pet​/{petId} DELETE /pet​/{petId} POST /store​/order GET /store​/order​/{orderId} DELETE /s

我很难正确地排序一组对象。这是排序条件的预期结果和解释:

Test data:

POST   /pet​/{petId}​/uploadImage
POST   /pet
PUT    /pet
GET    /pet​/findByStatus
GET    /pet​/{petId}
POST   /pet​/{petId}
DELETE /pet​/{petId}
POST   /store​/order
GET    /store​/order​/{orderId}
DELETE /store​/order​/{orderId}
GET    /store​/inventory

Step 1 - Sort on depth (amount of backslashes in the string)

POST   /pet
PUT    /pet
GET    /pet​/{petId}
POST   /pet​/{petId}
DELETE /pet​/{petId}
GET    /pet​/findByStatus
POST   /store​/order
GET    /store​/inventory
GET    /store​/order​/{orderId}
DELETE /store​/order​/{orderId}
POST   /pet​/{petId}​/uploadImage

Step 2 - Sort alphabetically regardless of string length

POST   /pet
PUT    /pet
GET    /pet​/findByStatus
GET    /pet​/{petId}
POST   /pet​/{petId}
DELETE /pet​/{petId}
POST   /pet​/{petId}​/uploadImage
GET    /store​/inventory
POST   /store​/order
GET    /store​/order​/{orderId}
DELETE /store​/order​/{orderId}

Step 3 - Sort identical strings based on their request-method in order POST, PUT, GET, DELETE

POST   /pet
PUT    /pet
GET    /pet​/findByStatus
POST   /pet​/{petId}
GET    /pet​/{petId}
DELETE /pet​/{petId}
POST   /pet​/{petId}​/uploadImage
GET    /store​/inventory
POST   /store​/order
GET    /store​/order​/{orderId}
DELETE /store​/order​/{orderId}
目前,我正在尝试执行步骤1和2。第1步是可行的,但我想我在执行第2步时使用了错误的逻辑,因为我没有按字母顺序对字符串进行排序,而不考虑长度,第3步我很困惑,因为我需要根据四个条件(POST、PUT、GET、DELETE),而不是排序函数所期望的两个条件。代码如下:

函数main(){
设arr=[];
arr.push({“路径”:“/pet”,“方法”:“POST”});
arr.push({“路径”:“/pet”,“方法”:“PUT”});
arr.push({“路径”:“/存储​/顺序“,”方法“:”后“});
arr.push({“路径”:“/存储​/存货,“方法”:“获取”});
arr.push({“路径”:“/存储​/秩序​/{orderId},“方法”:“GET”});
arr.push({“路径”:“/存储​/秩序​/{orderId},“方法”:“删除”});
arr.push({“路径”:“/pet​/{petId}​/上传图像,“方法”:“POST”};
arr.push({“路径”:“/pet​/{petId},“方法”:“GET”});
arr.push({“路径”:“/pet​/{petId},“方法”:“POST”};
arr.push({“路径”:“/pet​/{petId},“方法”:“删除”};
arr.push({path”:“/pet​/findByStatus,“方法”:“GET”});
arr.sort(函数(a,b){
常量aCount=(a.path.match(//\//g)| |[])长度;
常量bCount=(b.path.match(//\//g)| |[]).length;
如果(A计数B计数){
返回1;
}否则{
const aChars=a.path.split(“”.sort().join(“”);
常量bChars=b.path.split(“”.sort().join(“”);
if(aCharsbChars){
返回1
}否则{
返回0;
}
}
});
用于(arr的常数x){
console.log(x.path);
}
}

main()您需要颠倒代码中的排序顺序,即从步骤3开始,然后从步骤2开始,然后从步骤1开始。这样,结果数组将从步骤1、步骤2、步骤3排序。下面是一个简单的例子

const arr=[{“path”:“/pet”,“方法”:“POST”},{“path”:“/pet”,“方法”:“PUT”},{“path”:“/store/order”,“方法”:“GET”},{“path”:“/store/order/{orderId}”,“方法”:“GET”},{“path”:“/store order/{orderId}”,“方法”:“DELETE”},{“path”:“DELETE”},{“path”:“{pet pet pet/{petId}/uploadImage”,“方法”:“POST”},{path”:“POST”//orderId/{pet path},{:“GET”},{“path”:“/pet/{petId}”,“method”:“POST”},{“path”:“/pet/{petId}”,“method”:“DELETE”},{“path”:“/pet/findByStatus”,“method”:“GET”}]
常量多端口=(arr)=>
arr.sort({method:a},{method:b})=>{
const sortOrder=['POST','PUT','GET','DELETE'];
返回sortOrder.indexOf(a)>sortOrder.indexOf(b)?1:-1
})
.sort(({path:a},{path:b})=>a
(a.匹配(//\//g)| |[])。长度<(b.匹配(//\//g)| |[])。长度?-1:1)
函数main(){
控制台日志(多端口(arr))
} 

main()我给你做了一个片段。它不反映您发布的“实际输出”,它看起来不像是实现了排序的步骤3。@jfriend00它在哪里没有实现<代码>{“路径”:“/pet/{petId}”,“方法”:“删除”},{“路径”:“/pet/{petId}”,“方法”:“获取”},{“路径”:“/pet/{petId}”,“方法”:“发布”},
示例这里它排序了删除然后获取然后发布OK,没有看到那个部分。谢谢!但预期的CRUD顺序是POST、PUT、GET和DELETE。这里是另一条路。您可以将方法排序反转为(a