---
title: Table.Sort – เรียงลำดับข้อมูลในตาราง
url: https://www.thepexcel.com/functions/power-query/table-functions/table-sort/
type: function-explainer
program: Power Query
syntax: "Table.Sort(table as table, comparisonCriteria as any) as table"
date: 2025-12-03
updated: 2025-12-20
scores:
  popularity: 9
  difficulty: 2
  usefulness: 9
---

# Table.Sort – เรียงลำดับข้อมูลในตาราง

> เรียงลำดับข้อมูลในตารางตามคอลัมน์ที่ระบุ รองรับการเรียงหลายระดับและกำหนดทิศทางการเรียง

## คำอธิบาย

เรียงลำดับข้อมูลในตารางตามคอลัมน์ที่ระบุ รองรับการเรียงหลายระดับและกำหนดทิศทางการเรียง

## Syntax

```excel
Table.Sort(table as table, comparisonCriteria as any) as table
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table | Yes | Table |  | ตารางที่ต้องการเรียงลำดับ |
| comparisonCriteria | Yes | List |  | เกณฑ์การเรียงลำดับ อาจเป็น:  1) ชื่อคอลัมน์เดียว (text) = เรียง ascending เป็นค่าเริ่มต้น  2) List ของ text = เรียงหลายคอลัมน์ตามลำดับ  3) List ของ {text, Order} = ระบุทิศทาง Order.Ascending หรือ Order.Descending |

## ตัวอย่าง

### 1. เรียงตามคอลัมน์เดียว (Ascending)

```excel
let
    Sales = #table(
        {"OrderID", "Amount"},
        {{3, 500}, {1, 1200}, {2, 800}}
    ),
    Sorted = Table.Sort(Sales, {"OrderID"})
in
    Sorted
```

**ผลลัพธ์:** `| OrderID | Amount |
|---------|--------|
| 1       | 1200   |
| 2       | 800    |
| 3       | 500    |`

เรียงตาม OrderID จากน้อยไปมากแบบอัตโนมัติ (1, 2, 3) - ส่วนใหญ่เราใช้แบบนี้เวลาต้องเรียงลำดับที่เป็นตัวเลข

### 2. เรียงลดลง (Descending) + ระบุ Order.Descending

```excel
let
    Sales = #table(
        {"OrderID", "Amount"},
        {{3, 500}, {1, 1200}, {2, 800}}
    ),
    Sorted = Table.Sort(Sales, {{"Amount", Order.Descending}})
in
    Sorted
```

**ผลลัพธ์:** `| OrderID | Amount |
|---------|--------|
| 1       | 1200   |
| 2       | 800    |
| 3       | 500    |`

เรียงตาม Amount จากมากไปน้อย ด้วย Order.Descending - ใช้เมื่อต้องการเห็นจำนวนที่มากที่สุดก่อน

### 3. เรียงหลายคอลัมน์ (Multi-Level Sort)

```excel
let
    Sales = #table(
        {"Region", "Month", "Sales"},
        {{"North", "Jan", 100}, {"South", "Jan", 250}, {"North", "Feb", 150}, {"South", "Feb", 200}}
    ),
    Sorted = Table.Sort(
        Sales,
        {{"Region", Order.Ascending}, {"Month", Order.Ascending}, {"Sales", Order.Descending}}
    )
in
    Sorted
```

**ผลลัพธ์:** `| Region | Month | Sales |
|--------|-------|-------|
| North  | Feb   | 150   |
| North  | Jan   | 100   |
| South  | Feb   | 200   |
| South  | Jan   | 250   |`

เรียงแบบ 3 ระดับ: ตาม Region ก่อน (A-Z) แล้วตาม Month ถ้า Region เหมือนกัน สุดท้ายตาม Sales จากมากไปน้อย - เหมาะสำหรับรายงานที่ต้องจัดกลุ่มตามพื้นที่

### 4. เรียงข้อมูลตัวอักษร (Case Sensitive)

```excel
let
    Names = #table(
        {"Name"},
        {{"zebra"}, {"Apple"}, {"banana"}, {"Cherry"}}
    ),
    Sorted = Table.Sort(Names, {"Name"})
in
    Sorted
```

**ผลลัพธ์:** `| Name   |
|--------|
| Apple  |
| Cherry |
| banana |
| zebra  |`

Power Query เรียงแบบ case-sensitive ตัวใหญ่มาก่อนตัวเล็ก ถ้าต้อง case-insensitive ให้แปลงเป็นตัวเล็กก่อนเรียง หรือใช้ Comparer

## หมายเหตุเพิ่มเติม

- {'title': 'ใช้ Table.Sort หลายครั้งแบบต่อเรียง', 'explanation': 'Table.Sort(Table.Sort(Sales, {"Month"}), {"Region"}) - เรียงแบบนี้ได้ แต่ไม่ฝ่ายไหนครั้งเดียว เพราะการเรียงครั้งที่สองจะทำลายการเรียงครั้งแรก ถ้าต้องเรียงหลายระดับให้ทำในเครื่องเดียวดี'}

- {'title': 'เรียงแบบ Case-Insensitive', 'explanation': 'ถ้าต้องเรียง Text แบบไม่สนตัวพิมพ์ให้แปลงเป็นตัวเล็ก: Table.TransformColumns(Sales, {{"Name", Text.Lower}}) เสร็จแล้วค่อยเรียง หรือใช้ Comparer.OrdinalIgnoreCase แต่ซับซ้อนกว่า'}

- {'title': 'Stable Sort ด้วย Index', 'explanation': 'ถ้าต้อง stable sort (รักษาลำดับเดิมเมื่อค่าเหมือนกัน) ให้เพิ่มคอลัมน์ index: Table.AddIndexColumn(Sales, "Index", 0, 1) แล้วเรียง โดยใส่ {"Index", Order.Ascending} ไว้ด้านท้าย ซึ่งจะรักษาลำดับเดิมเมื่อค่าหลักเหมือนกัน'}

- {'title': 'ใช้ Table.Sort ใกล้สุดของการแปลง', 'explanation': 'เรียงข้อมูลตรงสุดท้ายของการแปลง (เต่าแนวสุดท้าย) ก่อน return ความเร็วจะดีขึ้น เพราะถ้าเรียงกลางๆ ขั้นตอนต่อไปอาจทำลายการเรียงแล้วต้องเรียงใหม่'}

- {'title': 'Order.Ascending คือค่าเริ่มต้น', 'explanation': 'ถ้าไม่ระบุ Order บริบสุมใจจะเป็น Order.Ascending สามารถเขียน {"Amount"} แทน {{"Amount", Order.Ascending}} ได้เลย'}

## คำถามที่พบบ่อย

**Q: ฉันต้องใช้ List เล็กน้อยสำหรับ comparisonCriteria ถึงจะไปได้?**

ครับ ถ้าเป็นการเรียงแบบเดียวอันเดียวสามารถใช้ {"ColumnName"} หรือ {{"ColumnName", Order.Ascending}} ถ้าเรียงหลายคอลัมน์ต้องเป็น {{"Col1", Order.Asc}, {"Col2", Order.Desc}} - เป็น list ของ list นั่นเอง

**Q: ความแตกต่างระหว่าง Order.Ascending และ Order.Descending คืออะไร?**

Order.Ascending = จากน้อยไปมาก (A-Z หรือ 0-9) / Order.Descending = จากมากไปน้อย (Z-A หรือ 9-0) - ส่วนใหญ่ใช้ Descending เมื่อต้องการเห็นรายการสำคัญก่อน เช่น ยอดขายสูงสุด

**Q: Table.Sort จะรักษาลำดับแถวเดิมไว้ไหมถ้าค่าในคอลัมน์เรียงเหมือนกัน?**

ไม่แน่นอน Power Query ไม่รับประกันลำดับแถวเดิม (stable sort) ถ้าสำคัญเก่ามากต้องเพิ่มคอลัมน์ index ก่อน เรียงตามค่าของอันแรก แล้วเรียงตามค่า index เป็นระดับสุดท้าย

**Q: ถ้าคอลัมน์มีค่า null หรือเป็นค่าว่าง Table.Sort จะจัดการอย่างไร?**

ค่า null จะถูกเรียงไปไว้ที่ด้านหน้าเสมอ (ก่อนค่าปกติ) ถ้าต้องจัดการพิเศษต้องใช้ Table.SelectRows เพื่อแยกแถว null ออกก่อน แล้วต่อรวมกลับหลังจากเรียง

## ฟังก์ชันที่เกี่ยวข้อง

- [Table.ReverseRows – กลับลำดับแถวจากล่างขึ้นบน](https://www.thepexcel.com/functions/power-query/table-functions/table-reverserows/)
- [Table.SelectRows – กรองแถวตามเงื่อนไขใน Power Query](https://www.thepexcel.com/functions/power-query/table-functions/table-selectrows/)
- [Table.TransformColumns – แปลงคอลัมน์ด้วยฟังก์ชันการแปลง](https://www.thepexcel.com/functions/power-query/table-functions/table-transformcolumns/)
- [Table.AddIndexColumn – เพิ่มคอลัมน์ลำดับเลข](https://www.thepexcel.com/functions/power-query/table-functions/table-addindexcolumn/)

## แหล่งข้อมูลเพิ่มเติม

- [Microsoft Docs - Table.Sort](https://learn.microsoft.com/en-us/powerquery-m/table-sort) _(official)_
- [PowerQuery.how - Table.Sort](https://powerquery.how/table-sort/) _(article)_

---

_Source: [https://www.thepexcel.com/functions/power-query/table-functions/table-sort/](https://www.thepexcel.com/functions/power-query/table-functions/table-sort/)_
