简体   繁体   English

JavaScript中的自定义数组排序

[英]Custom array sort in JavaScript

I have a task. 我有任务 Need to sort array of strings (cities) in following way: 需要通过以下方式对字符串(城市)数组进行排序:

  • First city from array is randomly chosen. 数组中的第一个城市是随机选择的。
  • Next city must begins with last letter of previous city. 下一个城市必须以上一个城市的最后一个字母开头。
  • If there is not such city, take random city again. 如果没有这样的城市,请再次选择随机城市。

The question is: what type of loop should i use and how to implement sorting? 问题是:我应该使用哪种类型的循环以及如何实现排序? Should i use Array.sort() method, and how i could transform original array of cities to new array dynamically. 我应该使用Array.sort()方法,以及如何将城市的原始数组动态转换为新数组。 Which methods of Array.prototype should i use? 我应该使用哪种Array.prototype方法?

let cities = [ "New York", "Tokio", "Moscow", "London", "Los Angeles", "Paris", "Berlin", "Madrid", "Kiev", "Oslo", "Barcelona", "Washington", "Ankara", "Rome", "Prague", "Amsterdam", "Minsk"];

function getRandomCity(arr) {
    return arr[Math.floor(Math.random()*arr.length)];
}

function sort(arr) {
    let unsortedArray = [...arr]; 
    let sortedArray = [];
}

 let cities = ["New York", "Tokio", "Moscow", "London", "Los Angeles", "Paris", "Berlin", "Madrid", "Kiev", "Oslo", "Barcelona", "Washington", "Ankara", "Rome", "Prague", "Amsterdam", "Minsk"]; function getRandomCity(arr) { return Math.floor(Math.random() * arr.length); } function sort(arr) { let unsortedArray = [...arr]; let sortedArray = []; let char = "!"; // choose will take an index and then push the city at that index into the sortedArray and remove it from unsortedArray and store the last letter of that city in char (uppercased) function choose(index) { let city = unsortedArray.splice(index, 1)[0]; sortedArray.push(city); char = city.charAt(city.length - 1).toUpperCase(); } choose(getRandomCity(unsortedArray)); // first of all, choose a random city while (unsortedArray.length) { // while there still cities let index, test = unsortedArray.some(function(c, i) { // check if there is a city that begin with char index = i; // store the index in the process return c.charAt(0) === char; }); if(test) // if we found a city choose(index); // choose it else // if not choose(getRandomCity(unsortedArray)); // choose a random one } return sortedArray; } console.log(sort(cities)); 

You can use Array.prototype.slice() , Array.prototype.splice() , recursion to return random element from array, or element having first letter, case insensitive, same as previous element 您可以使用Array.prototype.slice()Array.prototype.splice() ,递归从数组中返回随机元素,或者返回具有第一个字母(不区分大小写)的元素,与上一个元素相同

 let cities = [ "New York", "Tokio", "Moscow", "London" , "Los Angeles", "Paris", "Berlin", "Madrid" , "Kiev", "Oslo", "Barcelona", "Washington" , "Ankara", "Rome", "Prague", "Amsterdam", "Minsk"]; let copyCities = cities.slice(0); function getRandomCity(copy, len, res = []) { let curr, next; if (res.length > 0) { next = copy.filter(function(city) { var prev = res[res.length -1].slice(-1); return new RegExp(city[0], "i").test(prev) }); if (next.length) next = copy.splice(copy.indexOf(next.shift()), 1); } if (copy.length === len || !next.length) { res.push(copy.splice(Math.floor(Math.random()*copy.length), 1)[0]); } else { res.push(next[0]); } if (res.length < len) { return getRandomCity(copy, len, res) } else { return res } } var _cities = getRandomCity(copyCities, cities.length); console.log(_cities); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM