繁体   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