Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Sort to sort in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to use Sort to sort in Java. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. Array Sort sorting

Sort in ascending order, using the Arrays.Sort method directly, for example:

Import java.util.Arrays;public class Solution {public static void main (String [] args) {int [] array = {10, 3, 6, 1, 4, 5, 9}; / positive sort Arrays.sort (array) / / merge sort is used when the number of arrays is greater than 286 and continuity is good. If it is less than 47, insert sort is used, and System.out.println ("ascending sort:") is used for the rest; for (int num: array) {System.out.println (sort);}

Sort in descending order, which can be looped in reverse order in the case of outputting only the array, for example:

Import java.util.Arrays;public class Solution {public static void main (String [] args) {int [] array = {10, 3, 6, 1, 4, 5, 9}; / positive sort Arrays.sort (array) / / merge sort is used when the number of arrays is greater than 286 and continuity is good. If it is less than 47, insert sort is used, and System.out.println ("ascending sort:") is used for the rest of the cases; for (int num: array) {System.out.println (sort) } / / sort in reverse / / (1) since no reverse method is provided, you can reverse the output System.out.println ("descending output:"); for (int I = array.length-1; I > = 0; iMube -) {System.out.println (array [I]);}

Sort in descending order, for cases where you need to use an array, you can create a new array and then access the assignment in reverse order, for example:

Import java.util.Arrays;public class Solution {public static void main (String [] args) {int [] array = {10, 3, 6, 1, 4, 5, 9}; / positive sort Arrays.sort (array) / / merge sort is used when the number of arrays is greater than 286 and continuity is good. If it is less than 47, insert sort is used, and System.out.println ("ascending sort:") is used for the rest of the cases; for (int num: array) {System.out.println (sort) } / / sort in reverse / / (1) since no reverse method is provided, you can reverse the output System.out.println ("descending output:"); for (int I = array.length-1; I > = 0; iMube -) {System.out.println (array [I]) } / / (2) or create a new array and save it backwards to the new array int [] descArray = new int [array.length]; for (int I = 0; I < array.length; iArray +) {descArray [I] = array [array.length-I-1];} System.out.println ("New array descending output:") For (int num: descArray) {System.out.println (num);} above is how to use Sort to sort the Java shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report