2017-04-05 93 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);