အော်ချို့အုပ်စုကို ပြောင်းလဲတာ ဘာကြောင်း

ဂျူနိုင်ဂျီယူင်း ကို ဟိုင်တာ အော်ချို့အုပ်စုကို ပြောင်းလဲပြီး တုံ့ပြန်ကြည့်တယ်

ဘေ့စ်ခုချို့ကို လုပ်ကန့်ပြီး သူ့အမည်အဖြစ် အစားအပြားအုပ်စုအပေါ် အဲဒီအော်ချို့အချိန်အတိုင်း အော်ချို့ကို အော်ချို့အမည်အော်အော်ချို့ကို အော်ချို့ကို ပြောင်းလဲပြီး

Name Country
Berglunds snabbkop Sweden
North/South UK
Alfreds Futterkiste Germany
Koniglich Essen Germany
Magazzini Alimentari Riuniti Italy
Paris specialites France
Island Trading UK
Laughing Bacchus Winecellars Canada

亲自试一试

ပြောင်းလဲပြီး ပုံစံကုသပ်သတ်သည်

အမှတ်

function sortTable() {
  var table, rows, switching, i, x, y, shouldSwitch;
  table = document.getElementById("myTable");
  switching = true;
  /* Make a loop until the condition is met. */
  no switching has been done: */
  while (switching) {
    switching = false;
    rows = table.rows;
    /* အချက်အလက် အချက်အလက် အချက်အလက် တို့ ကို အားလုံး ကြည့်လိမ့်မည် သော်လည်း ပထမ အချက်အလက် ကို အားလုံး ကြည့် မပါ */
    /* Traverse all table rows (except the first row, which contains the header): */
    // ပထမ အချက်အလက် သည် အချက်အလက် အရာ ပါဝင်သည် ဟု ပြောပြီ
      // First declare that no exchange should be made:
      /* သင့် သင့် ကြည့်ရန် အချက်အလက် တို့ ကို ရယူ လိမ့်မည် ကို ရယူ ပြီ */
      /* Get the two elements to be compared, one from the current row and one from the next row: */
      x = rows[i].getElementsByTagName("TD")[0];
      y = rows[i + 1].getElementsByTagName("TD")[0];
      // Check if the two rows should be swapped positions:
      else if (dir == "desc") {
        // If an exchange should be made, mark it as needing an exchange and break out of the current loop
        shouldSwitch = true;
        break;
      }
    }
    if (shouldSwitch) {
      /* အမှတ်အသား လုပ်ဆောင်လိမ့်မည် သော် အစားထိုးလိမ့်မည် ကို လုပ်ဆောင် လိမ့်မည် ဖြစ်သည် ကို မှတ်သားပြီ */
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
      switching = true;
    }
  }
}

亲自试一试

Sort the table by clicking on the table header

Click "Name" to sort by name, click "Country" to sort by country.

The sorting order is ascending (from A to Z) when clicked for the first time.

Click again, the sorting order will be descending (from Z to A):

Name Country
Berglunds snabbkop Sweden
North/South UK
Alfreds Futterkiste Germany
Koniglich Essen Germany
Magazzini Alimentari Riuniti Italy
Paris specialites France
Island Trading UK
Laughing Bacchus Winecellars Canada

အမှတ်

<table id="myTable2">
<tr>
<!-- When the table header is clicked, run the sortTable function and pass a parameter, 0 means sort by name, 1 means sort by country: -->
<th onclick="sortTable(0)">Name</th>
<th onclick="sortTable(1)">Country</th>
</tr>
...
<script>
function sortTable(n) {
  var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
  table = document.getElementById("myTable2");
  switching = true;
  // 设置排序方向为升序:
  dir = "asc";
  /* 创建一个循环,该循环将持续进行,直到没有行需要交换为止: */
  while (switching) {
    switching = false;
    rows = table.rows;
    /* အချက်အလက် အချက်အလက် အချက်အလက် တို့ ကို အားလုံး ကြည့်လိမ့်မည် သော်လည်း ပထမ အချက်အလက် ကို အားလုံး ကြည့် မပါ */
    for (i = 1; i < (rows.length - 1); i++) {
    // ပထမ အချက်အလက် သည် အချက်အလက် အရာ ပါဝင်သည် ဟု ပြောပြီ
      shouldSwitch = false;
      /* သင့် သင့် ကြည့်ရန် အချက်အလက် တို့ ကို ရယူ လိမ့်မည် ကို ရယူ ပြီ */
      x = rows[i].getElementsByTagName("TD")[n];
      y = rows[i + 1].getElementsByTagName("TD")[n];
      /* ဒါတို့ အချက်အလက် တို့ ကြား သွားကြောင်း မှာ လုပ်ဆောင်သင့် သည် ကို စီစဥ် လိမ့်မည် ကို သိမှတ် ပြီ မှာ အားလုံး မှာ တိုးမှား သို့မဟုတ် ကွေးကွေး ဖြစ် */
      if (dir == "asc") {
      if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
        else if (dir == "desc") {
          // လုပ်ဆောင်သင့် သော် အမှတ်အသား လုပ်ဆောင်လိမ့်မည် ဖြစ်သည် မှတ်ပြီ အပြီး လက်ရှိ ချိန်နှင့် ချိန်အား လွှတ်
          shouldSwitch = true;
          break;
        }
      }
        if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
          // လုပ်ဆောင်သင့် သော် အမှတ်အသား လုပ်ဆောင်လိမ့်မည် ဖြစ်သည် မှတ်ပြီ အပြီး လက်ရှိ ချိန်နှင့် ချိန်အား လွှတ်
          shouldSwitch = true;
          break;
        }
      }
    }
    if (shouldSwitch) {
      /* အမှတ်အသား လုပ်ဆောင်လိမ့်မည် သော် အစားထိုးလိမ့်မည် ကို လုပ်ဆောင် လိမ့်မည် ဖြစ်သည် ကို မှတ်သားပြီ */
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
      switching = true;
      // မောင်းကန် အနည်းငယ် အချက်အလက် အတွက် ပြင်ဆင် လိမ့်မည်
      switchcount ++;
    }
      /* မောင်းကန်မရှိဘဲ စီစဥ်အရာဖြစ် သော် စီစဥ်အရာ "တိုးမှား" ကို တစ်ကြိမ်တာ ပြန်လည် အလုပ်လုပ် လိမ့်မည် */
      if (switchcount == 0 && dir == "asc") {
        dir = "desc";
        switching = true;
      }
    }
  }
}
</script>

亲自试一试

စားလုံးအချက်အလက်ကို နည်းပါးအောင် စီစဥ်ထား

အမှတ်

if (Number(x.innerHTML) > Number(y.innerHTML)) {
  shouldSwitch = true;
  break;
}

亲自试一试