簡體   English   中英

數組中對象的 JavaScript 冒泡和插入排序

[英]JavaScript Bubble & Insertion sort for objects in an array

我正在嘗試按價格進行冒泡排序並按名稱插入排序,但我對如何打印排序列表感到困惑。 我是否只是因為我嘗試了 console.log 而它只是打印了數組但沒有對其進行排序? 我應該退貨嗎? (請不要介意格式我是新來的抱歉!)

    //Class 

      class Shoe{
          constructor(name, price, type) {
          this.name = name;
          this.price = price;
          this.type = type;
          }
     }

    // list of objects
    var shoes = [
        new Shoe('Nike AirMax 90', '120', 'Casual'),
        new Shoe('Jordan Retro 1', '110', 'Casual'),
        new Shoe('Jadon Doc Martens', '250', 'Seasonal boots'),
        new Shoe('Adidas X Ghosted', '110', 'Athletic'),
        new Shoe('Nike Vapourmax Flyknit', '250', 'Casual'),
        new Shoe('Aldo Loafers', '130', 'Formal'),
        new Shoe('Timberlands', '199', 'Seasonal boots'),
        new Shoe('Converse High Tops', '70', 'Casual'),
        new Shoe('Converse Low Tops', '80', 'Casual'),
        new Shoe('Adidas NMDs', '110', 'Athletic'),
        new Shoe('Heels', '130', 'Formal'),
        new Shoe('Nike AirForce', '150', 'Casual')
    ];

    // bubble sort
       function bubbleSort(shoes) {
         var swapped;
         do {
           swapped = false;
              for (var i=0; i < shoes.length-1; i++) {
                 if (shoes[i].price > shoes[i+1].price) {
                    var temp = shoes[i];
                    shoes[i] = shoes[i+1];
                    shoes[i+1] = temp;
                    swapped = true;
                 }
              }
          } while (swapped);
        }

       // insertion sort
       function insertionSort(shoes) {
          let a = shoes.length;
              for (let i = 1; i < a; i++) {
              // Choosing the first element in our unsorted subarray
                  let first = shoes[i];
        // The last element of our sorted subarray
                  let l = i-1; 
                  while ((l > -1) && (first.type < shoes[l].type)) {
                       shoes[l+1] = shoes[l];
                       l--;
                  }
                  shoes[l+1] = first;
               }
           return shoes;
        }

任何幫助真的很感激,謝謝!

排序函數可以通過改變/改變輸入來對輸入數據進行排序,或者通過獲取並返回一個排序的副本來對輸入數據進行排序,這使得原始數據保持不變。 這兩種類型都是可以接受的做事方式。

在您的示例中,您的兩種排序都會改變傳入的數據,因此雖然它們可以方便地返回數組(就像您的插入排序已經這樣做),但請記住,無論返回值是否為原始數組,仍然會更改記錄。

但是,我可以看到您在這里遇到的一個問題實際上是關於字符串轉換的 - 您需要將價格轉換為數字以正確比較它們。 目前,您正在執行字符串比較。

很多方法可以做到這一點,但我個人喜歡一元加號

 class Shoe { constructor(name, price, type) { this.name = name; this.price = price; this.type = type; } } var shoes = [ new Shoe('Nike AirMax 90', '120', 'Casual'), new Shoe('Jordan Retro 1', '110', 'Casual'), new Shoe('Jadon Doc Martens', '250', 'Seasonal boots'), new Shoe('Adidas X Ghosted', '110', 'Athletic'), new Shoe('Nike Vapourmax Flyknit', '250', 'Casual'), new Shoe('Aldo Loafers', '130', 'Formal'), new Shoe('Timberlands', '199', 'Seasonal boots'), new Shoe('Converse High Tops', '70', 'Casual'), new Shoe('Converse Low Tops', '80', 'Casual'), new Shoe('Adidas NMDs', '110', 'Athletic'), new Shoe('Heels', '130', 'Formal'), new Shoe('Nike AirForce', '150', 'Casual') ]; function bubbleSort(shoes) { var swapped; do { swapped = false; for (var i = 0; i < shoes.length - 1; i++) { // Must convert prices to numbers, // otherwise they get compared as strings if (+shoes[i].price > +shoes[i + 1].price) { var temp = shoes[i]; shoes[i] = shoes[i + 1]; shoes[i + 1] = temp; swapped = true; } } } while (swapped); return shoes; } bubbleSort(shoes); console.log('Bubble Sort:\\n', shoes);

不確定您從頭開始編寫排序的原因。 不要誤會我的意思,我喜歡在很多東西上“推出我自己的”,但是如果你想做一個排序,你可以這樣做:

shoes.sort(function (a, b) {return a.price - b.price});

至於你顯示你的結果,我通常只是用一個 div 元素和一個按鈕制作一個簡單的 html 頁面。 當我單擊按鈕時,我的函數使用 div 的 innerText 屬性填充了結果。 請記住,將對象或數組發送到顯示器時,您可能希望使用 JSON.stringify 來獲取實際內容,而不僅僅是

[對象對象],[對象對象],[對象對象]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM