কম্পিউটার

জাভাস্ক্রিপ্ট বুদবুদ একটি অ্যারের বস্তুর জন্য সাজান


ধরুন, আমাদের একটি কনস্ট্রাক্টর ক্লাস আছে যেটি এইরকম −

শু অবজেক্ট তৈরি করে
class Shoe {
   constructor(name, price, type) {
      this.name = name;
      this.price = price;
      this.type = type;
   }
};

আমরা এই ক্লাসটি ব্যবহার করছি এই ধরনের বস্তু দিয়ে একটি অ্যারে পূরণ করতে -

const arr = [
   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')
];

আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা বস্তুর মূল্য সম্পত্তির উপর ভিত্তি করে বস্তুর এই বিন্যাস সাজানোর জন্য সঠিকভাবে বুদ্বুদ সাজানোর অ্যালগরিদম ব্যবহার করে।

উদাহরণ

এর জন্য কোড হবে −

class Shoe {
   constructor(name, price, type) {
      this.name = name;
      this.price = price;
      this.type = type;
   }
};
const arr = [
   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')
];
const bubbleSort = (arr = []) => {
   let swapped;
   do {
      swapped = false;
      for (let i = 0; i < arr.length − 1; i++) {
         if (+arr[i].price > +arr[i + 1].price) {
            let temp = arr[i];
            arr[i] = arr[i + 1];
            arr[i + 1] = temp;
            swapped = true;
         };
      };
   }while (swapped);
}
bubbleSort(arr);
console.log(arr);

আউটপুট

এবং কনসোলে আউটপুট হবে −

[
   Shoe { name: 'Converse High Tops', price: '70', type: 'Casual' },
   Shoe { name: 'Converse Low Tops', price: '80', type: 'Casual' },
   Shoe { name: 'Jordan Retro 1', price: '110', type: 'Casual' },
   Shoe { name: 'Adidas X Ghosted', price: '110', type: 'Athletic' },
   Shoe { name: 'Adidas NMDs', price: '110', type: 'Athletic' },
   Shoe { name: 'Nike AirMax 90', price: '120', type: 'Casual' },
   Shoe { name: 'Aldo Loafers', price: '130', type: 'Formal' },
   Shoe { name: 'Heels', price: '130', type: 'Formal' },
   Shoe { name: 'Nike AirForce', price: '150', type: 'Casual' },
   Shoe { name: 'Timberlands', price: '199', type: 'Seasonal boots' },
   Shoe { name: 'Jadon Doc Martens', price: '250', type: 'Seasonal boots'},
   Shoe { name: 'Nike Vapourmax Flyknit', price: '250', type: 'Casual' }
]

  1. জাভাস্ক্রিপ্ট - অ্যারে অবজেক্টের দৈর্ঘ্য

  2. জাভাস্ক্রিপ্টে Array.prototype.sort()।

  3. জাভাস্ক্রিপ্টে একটি অ্যারের সূচী অনুসারে সাজান

  4. জাভাস্ক্রিপ্টে মূল্য অনুসারে একটি অ্যারে সাজানো