JavaScript Array အမျိုးအစား()
ဥပမာများ
အခင်းအကျင်းကို စီရန်-
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
စီခွဲပြီးနောက် အစီအစဉ်ကို ပြောင်းပြန်လှန်ပါ-
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
fruits.reverse();
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
sort()
array တစ်ခု၏ အစိတ်အပိုင်းများကို အမျိုးအစားခွဲသည် ။
မူရင်း sort()
array ကို overwrite လုပ်သည်။
sort()
ဒြပ်စင်များကို အက္ခရာစဉ်အလိုက် မျဉ်းများအဖြစ် အမျိုးအစားခွဲသည် ။
ကိုလည်းကြည့်ပါ-
နှိုင်းယှဥ်လုပ်ဆောင်ချက်ကို စီရန်
အက္ခရာစဉ်အလိုက် စီခြင်းသည် ကြိုးများအတွက် ကောင်းမွန်စွာ အလုပ်လုပ်သည် ("Apple" သည် "Banana" ၏ ရှေ့တွင် ဖြစ်သည်)။
သို့သော် နံပါတ်များကို စီထားခြင်းသည် မှားယွင်းသောရလဒ်များကို ထုတ်ပေးနိုင်သည်။
"25" သည် "100" ထက်ကြီးသောကြောင့် "2" သည် "1" ထက်ကြီးသောကြောင့်ဖြစ်သည်။
"နှိုင်းယှဉ်လုပ်ဆောင်ချက်" ကိုပေးခြင်းဖြင့်၎င်းကိုသင်ဖြေရှင်းနိုင်သည် (အောက်ပါဥပမာများကိုကြည့်ပါ) ။
အထားအသို
array.sort(compareFunction)
ကန့်သတ်ချက်များ
Parameter | Description |
compareFunction |
Optional. A function that defines a sort order. The function should return a negative, zero, or positive value, depending on the arguments:
When sort() compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value. Example: The sort function will sort 40 as a value lower than 100. When comparing 40 and 100, sort() calls the function(40,100). The function calculates 40-100, and returns -60 (a negative value). |
တန်ဖိုးကို ပြန်ပေးသည်။
စီစဥ်ထားသည့်အရာများဖြင့် ခင်းကျင်းထားသည်။ |
နောက်ထပ် ဥပမာများ
နံပါတ်များကို ငယ်စဉ်လိုက် စီပါ-
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b});
နံပါတ်များကို ကြီးစဉ်ငယ်လိုက် စီပါ-
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b-a});
အနိမ့်ဆုံးတန်ဖိုးကိုရှာပါ
const points = [40, 100, 1, 5, 25, 10];
// Sort the numbers in ascending order
points.sort(function(a, b){return a-b});
let lowest = points[0];
အမြင့်ဆုံးတန်ဖိုးကို ရှာပါ
const points = [40, 100, 1, 5, 25, 10];
// Sort the numbers in descending order:
points.sort(function(a, b){return b-a});
let lowest = points[0];
အမြင့်ဆုံးတန်ဖိုးကို ရှာပါ
const points = [40, 100, 1, 5, 25, 10];
// Sort the numbers in ascending order:
points.sort(function(a, b){return a-b});
let highest = points[points.length-1];
Browser ပံ့ပိုးမှု
sort()
ECMAScript1 (ES1) အင်္ဂါရပ်တစ်ခုဖြစ်သည်။
ES1 (JavaScript 1997) ကို ဘရောက်ဆာအားလုံးတွင် အပြည့်အဝ ပံ့ပိုးထားသည်။
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |