2017-04-05 56 views
0

我有一个任务。需要按以下方式对字符串(城市)进行排序:JavaScript中的自定义数组排序

  • 从数组中随机选择第一个城市。
  • 下一个城市必须从前一个城市的最后一个字母开始。
  • 如果没有这样的城市,再次采取随机的城市。

问题是:我应该使用什么类型的循环以及如何实现排序?我应该使用Array.sort()方法,以及如何将原始城市数组动态地转换为新数组。我应该使用哪些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 = []; 
} 
+0

使用'Array.prototype.sort'来做它和治愈癌症一样困难!你将不得不使用循环! –

+0

如果没有为你解决这个问题很难说,但我认为[一些](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some)或[filter] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)可能对您有用。 – jmargolisvt

回答

0

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));

0

可以使用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);