All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What are ASC and DESC? What is the difference? Examples of use in SQL, JavaScript, and Ruby.

This content has been automatically translated from Ukrainian.
В чому різниця між ASC / DESC
В чому різниця між ASC / DESC
What is ASC sorting?
ASC (Ascending) sorting is a method of arranging elements in a sequence (for example, in an array or list) in increasing order. It is based on comparing elements and rearranging them to achieve the appropriate order.
The ASC sorting process involves sequentially comparing two adjacent elements and swapping them if they do not match the appropriate order. In each pass, the largest (or smallest) element is placed in its correct position. This process is repeated for all elements until complete ordering is achieved.
ASC sorting is one of the simplest sorting algorithms, but its efficiency depends on the size of the input data. In the case of a large number of elements, it may be inefficient compared to other algorithms, such as quicksort (QuickSort) or merge sort (MergeSort), which have better complexity.
How to perform ASC sorting in SQL?
SELECT column_name
FROM table_name
ORDER BY column_name ASC;
How to perform ASC sorting in JS (JavaScript)?
const arr = [5, 2, 8, 1, 6];
arr.sort((a, b) => a - b);
console.log(arr);
How to perform ASC sorting in Ruby?
arr = [5, 2, 8, 1, 6]
arr.sort!
puts arr
What is DESC sorting?
DESC (Descending) sorting is a method of arranging elements in a sequence (for example, in an array or list) in decreasing order. It is the opposite of ASC sorting, as the elements are arranged in reverse order.
The DESC sorting process involves comparing two adjacent elements and rearranging them if they do not match the appropriate order. In each pass, the smallest (or largest) element is placed in its correct position. This process is repeated for all elements until complete ordering is achieved.
DESC sorting is also one of the simplest sorting algorithms. In many programming languages and database management systems (for example, SQL), DESC is the default sorting order unless other settings are specified.
How to perform DESC sorting in SQL?
SELECT column_name
FROM table_name
ORDER BY column_name DESC;
How to perform DESC sorting in JS (JavaScript)?
const arr = [5, 2, 8, 1, 6];
arr.sort((a, b) => b - a);
console.log(arr);
How to perform DESC sorting in Ruby?
arr = [5, 2, 8, 1, 6]
arr.sort! { |a, b| b <=> a }
puts arr
ASC and DESC are the simplest sorting methods. To remember the direction of sorting - just refer to the corresponding English words. ASC (Ascending) and DESC (Descending).

This post doesn't have any additions from the author yet.

25 Jun 13:30

What is RST with Optane and RST Premium with Optane, and what is the difference? What are the differences from AHCI?

meme code
meme code@memecode
26 Jun 03:43

What is the difference between SSD and HDD?

meme code
meme code@memecode
26 Jun 03:48

What is NVMe (Non-Volatile Memory Express)? Where is NVMe used?

meme code
meme code@memecode
26 Jun 03:52

What is PCIe (Peripheral Component Interconnect Express)?

meme code
meme code@memecode
26 Jun 05:44

What is Ubuntu Pro? What is the difference between Ubuntu and Ubuntu Pro?

meme code
meme code@memecode
26 Jun 05:44

Final stage of installing Ubuntu. Post-installation configuration of Ubuntu.

meme code
meme code@memecode
30 Jun 10:07

What is QuickSort? When and by whom was it created? How does QuickSort work?

meme code
meme code@memecode
30 Jun 10:50

What is Recursion? An example of recursion in real life. The consequences of infinite recursion.

meme code
meme code@memecode
30 Jun 12:08

What is MergeSort? When and by whom was it created? How does MergeSort work?

meme code
meme code@memecode
30 Jun 12:15

What is the difference between QuickSort and MergeSort?

meme code
meme code@memecode
03 Jul 05:03

What is MS-DOS? When and by whom was MS-DOS created?

meme code
meme code@memecode
03 Jul 06:45

What is an API (Application Programming Interface)?

meme code
meme code@memecode