কম্পিউটার টিউটোরিয়াল

জাভাস্ক্রিপ্টে অ্যারেকে সর্ট অর্ডার হিসেবে ব্যবহার করা

const sort = ["this","is","my","custom","order"];
const myObjects = [
  {"id":1,"content":"is"},
  {"id":2,"content":"my"},
  {"id":3,"content":"this"},
  {"id":4,"content":"custom"},
  {"id":5,"content":"order"}
];

আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা দুটি такой অ্যারে গ্রহণ করে এবং দ্বিতীয় অবজেক্টের অ্যারেটিকে প্রথম অ্যারেতি অনুযায়ী সর্ট করবে যাতে অবজেক্টগুলোর content প্রপার্টি প্রথম অ্যারেতে স্ট্রিংগুলোর সাথে মেলে।

অতএব, উপরের অ্যারেগুলোর জন্য আউটপুট হবে:

const output = [
  {"id":3,"content":"this"},
  {"id":1,"content":"is"},
  {"id":2,"content":"my"},
  {"id":4,"content":"custom"},
  {"id":5,"content":"order"}
];

উদাহরণ

কোডটি হবে:

const arrLiteral = ["this","is","my","custom","order"];
const arrObj = [
  {"id":1,"content":"is"},
  {"id":2,"content":"my"},
  {"id":3,"content":"this"},
  {"id":4,"content":"custom"},
  {"id":5,"content":"order"}
];
const sortByReference = (arrLiteral, arrObj) => {
  const sorted = arrLiteral.map(el => {
    for(let i = 0; i < arrObj.length; ++i){
      if(arrObj[i].content === el){
        return arrObj[i];
      }
    };
  });
  return sorted;
};
console.log(sortByReference(arrLiteral, arrObj));

আউটপুট

কনসোলে আউটপুট হবে:

[
  { id: 3, content: 'this' },
  { id: 1, content: 'is' },
  { id: 2, content: 'my' },
  { id: 4, content: 'custom' },
  { id: 5, content: 'order' }
]
  1. জাভাস্ক্রিপ্টে Array.prototype.sort() মেথড

  2. জাভাস্ক্রিপ্টে ইনসারশন সোর্ট ব্যবহার করে সংখ্যার অ্যারে ক্রমবর্ধমান সাজানো

  3. অ্যারেকে আরোহী ক্রমে সাজানোর C প্রোগ্রাম

  4. অ্যারেকে অবরোহী ক্রমে সাজানোর C প্রোগ্রাম