logo

כיצד למיין מערך מחרוזות ב-Java

בתכנות, מִיוּן חשוב כי הוא שם אלמנטים של an מַעֲרָך בסדר מסוים. הסדר בשימוש נרחב הוא סדר אלפביתי אוֹ סדר טבעי . המיון משמש לקנוניזציה (תהליך המרת נתונים בצורה הסטנדרטית) נתונים ולהפקת פורמט הניתן לקריאה אנושית. בחלק זה נלמד כיצד למיין מערך מחרוזות ב-Java באמצעות לוגיקה מוגדרת על ידי המשתמש ו מערכים. סוג() שיטה

ישנן שתי דרכים למיין מערך מחרוזות ב-Java:

  • באמצעות משתמש הוגדר הִגָיוֹן
  • משתמש ב Arrays.sort() מתודם

שימוש בלוגיקה בהגדרת משתמש

נוכל למיין מערך מחרוזות על ידי השוואת כל אלמנט עם שאר האלמנטים. בדוגמה הבאה, עשינו את אותו הדבר. השתמשנו בשניים עבור לולאות. הלולאה הפנימית (השנייה) עבור מונעת את החזרות בהשוואה. אם התנאי (countries[i].compareTo(countries[j])>0) נכון מ-0, הוא מבצע את ההחלפה וממיין את המערך.

ג'אווה מתמטיקה עגולה

SortStringArrayExample1.java

 import java.util.Arrays; public class SortStringArrayExample1 { public static void main(String args[]) { //defining an array of type String String[] countries = {&apos;Zimbabwe&apos;, &apos;South-Africa&apos;, &apos;India&apos;, &apos;America&apos;, &apos;Yugoslavia&apos;, &apos; Australia&apos;, &apos;Denmark&apos;, &apos;France&apos;, &apos;Netherlands&apos;, &apos;Italy&apos;, &apos;Germany&apos;}; int size = countries.length; //logic for sorting for(int i = 0; i<size-1; i++) { for (int j="i+1;" j0) swapping array elements string temp="countries[i];" countries[i]="countries[j];" countries[j]="temp;" } prints the sorted in ascending order system.out.println(arrays.tostring(countries)); < pre> <p> <strong>Output:</strong> </p> <pre> [ Australia, America, Denmark, France, Germany, India, Italy, Netherlands, South-Africa, Yugoslavia, Zimbabwe] </pre> <h3>Using the Arrays.sort() Method</h3> <p>In Java, <strong>Arrays</strong> is the class defined in the java.util package that provides <strong>sort()</strong> method to sort an array in ascending order. It uses <strong>Dual-Pivot Quicksort algorithm</strong> for sorting. Its complexity is <strong>O(n log(n))</strong> . It is a <strong>static</strong> method that parses an <strong>array</strong> as a parameter and does not return anything. We can invoke it directly by using the class name. It accepts an array of type int, float, double, long, char, byte.</p> <p> <strong>Syntax:</strong> </p> <pre> public static void sort(int[] a) </pre> <p>Where <strong>a</strong> is an array to be short.</p> <h4>Note: Like the Arrays class, the Collections class also provides the sort() method to sort the array. But there is a difference between them. The sort() method of the Arrays class works for primitive type while the sort() method of the Collections class works for objects Collections, such as LinkedList, ArrayList, etc.</h4> <p>We can perform sorting in the following ways:</p> <ul> <tr><td>Ascending Order</td> or <strong>Alphabetical Order</strong> or <strong>Natural Order</strong>  </tr><tr><td>Descending Order</td> or <strong>Reverse Natural Order</strong>  </tr></ul> <h3>Sort String Array in Ascending Order or Alphabetical Order</h3> <p>The <strong>ascending order</strong> arranges the elements in the lowest to highest order. It is also known as <strong>natural order</strong> or <strong>alphabetical order</strong> .</p> <p>Let&apos;s sort an array using the sort() method of the Arrays class.</p> <p> <strong>SortStringArrayExample2.java</strong> </p> <pre> import java.util.Arrays; public class SortStringArrayExample2 { public static void main(String args[]) { //defining an array of type string String[] countries = {&apos;Wood apple&apos;, &apos;Blackberry&apos;, &apos;Date&apos;, &apos;Naseberry&apos;, &apos;Tamarind&apos;, &apos;Fig&apos;, &apos;Mulberry&apos;, &apos;Apple&apos;, &apos;Plum&apos;, &apos;Orange&apos;, &apos;Custard apple&apos;, &apos;Apricot&apos;}; //sorts string array in alphabetical order or ascending order Arrays.sort(countries); //prints the sorted string array in ascending order System.out.println(Arrays.toString(countries)); } } </pre> <p> <strong>Output:</strong> </p> <pre> [Apple, Apricot, Blackberry, Custard apple, Date, Fig, Mulberry, Naseberry, Orange, Plum, Tamarind, Wood apple] </pre> <h3>Sort String Array in Descending Order or Reverse Natural Order</h3> <h3>Using the reverseOrder() Method</h3> <p>Java <strong>Collections</strong> class provides the <strong>reverseOrder()</strong> method to sort the array in reverse-lexicographic order. It is a static method, so we can invoke it directly by using the class name. It does not parse any parameter. It returns a <strong>comparator</strong> that imposes the reverse of the natural ordering (ascending order).</p> <p>It means that the array sorts elements in the ascending order by using the sort() method, after that the reverseOrder() method reverses the natural ordering, and we get the sorted array in descending order.</p> <p> <strong>Syntax:</strong> </p> <pre> public static Comparator reverseOrder() </pre> <p>Suppose, a[] is an array to be sort in the descending order. We will use the reverseOrder() method in the following way:</p> <pre> Arrays.sort(a, Collections.reverseOrder()); </pre> <p>Let&apos;s sort a string array in the descending order.</p> <p> <strong>SortStringArrayExample3.java</strong> </p> <pre> import java.util.*; public class SortStringArrayExample1 { public static void main(String args[]) { //defining an array of type String String[] countries = {&apos;Zimbabwe&apos;, &apos;South-Africa&apos;, &apos;India&apos;, &apos;America&apos;, &apos;Yugoslavia&apos;, &apos; Australia&apos;, &apos;Denmark&apos;, &apos;France&apos;, &apos;Netherlands&apos;, &apos;Italy&apos;, &apos;Germany&apos;}; //sorts array in descending order Arrays.sort(countries, Collections.reverseOrder()); //prints the sorted string array in descending order System.out.println(Arrays.toString(countries)); } } </pre> <p> <strong>Output:</strong> </p> <pre> [Zimbabwe, Yugoslavia, South-Africa, Netherlands, Italy, India, Germany, France, Denmark, America, Australia] </pre> <hr></size-1;>

שימוש בשיטת Arrays.sort()

ב-Java, מערכים הוא המחלקה המוגדרת בחבילת java.util שמספקת סוג() שיטה למיין מערך בסדר עולה. זה משתמש אלגוריתם Quicksort Dual-Pivot למיון. המורכבות שלו היא O(n log(n)) . זה סטָטִי שיטה המנתחת את an מַעֲרָך כפרמטר ולא מחזיר כלום. אנו יכולים להפעיל אותו ישירות על ידי שימוש בשם הכיתה. הוא מקבל מערך מסוג int, float, double, long, char, byte.

תחביר:

 public static void sort(int[] a) 

איפה א הוא מערך להיות קצר.

הערה: כמו המחלקה Arrays, גם המחלקה Collections מספקת את שיטת sort() למיין את המערך. אבל יש הבדל ביניהם. שיטת sort() של המחלקה Arrays עובדת עבור סוג פרימיטיבי בעוד ששיטת sort() של המחלקה Collections עובדת עבור אובייקטים Collections, כגון LinkedList, ArrayList וכו'.

אנו יכולים לבצע מיון בדרכים הבאות:

    בסדר עולהאוֹ סדר אלפביתי אוֹ סדר טבעי סדר יורדאוֹ סדר טבעי הפוך

מיין מערך מחרוזות בסדר עולה או בסדר אלפביתי

ה בסדר עולה מסדר את האלמנטים בסדר הנמוך עד הגבוה ביותר. זה ידוע גם בשם סדר טבעי אוֹ סדר אלפביתי .

בואו נמיין מערך באמצעות שיטת sort() של המחלקה Arrays.

SortStringArrayExample2.java

מרכז תמונות ב-CSS
 import java.util.Arrays; public class SortStringArrayExample2 { public static void main(String args[]) { //defining an array of type string String[] countries = {&apos;Wood apple&apos;, &apos;Blackberry&apos;, &apos;Date&apos;, &apos;Naseberry&apos;, &apos;Tamarind&apos;, &apos;Fig&apos;, &apos;Mulberry&apos;, &apos;Apple&apos;, &apos;Plum&apos;, &apos;Orange&apos;, &apos;Custard apple&apos;, &apos;Apricot&apos;}; //sorts string array in alphabetical order or ascending order Arrays.sort(countries); //prints the sorted string array in ascending order System.out.println(Arrays.toString(countries)); } } 

תְפוּקָה:

 [Apple, Apricot, Blackberry, Custard apple, Date, Fig, Mulberry, Naseberry, Orange, Plum, Tamarind, Wood apple] 

מיין מערך מחרוזות בסדר יורד או סדר טבעי הפוך

שימוש בשיטת reverseOrder()

Java אוספים הכיתה מספקת את בסדר הפוך() שיטה למיין את המערך בסדר לקסיקוגרפי הפוך. זוהי שיטה סטטית, כך שאנו יכולים להפעיל אותה ישירות באמצעות שם המחלקה. זה לא מנתח שום פרמטר. זה מחזיר א משווה שכופה את ההפך מהסדר הטבעי (סדר עולה).

זה אומר שהמערך ממיין אלמנטים בסדר עולה על ידי שימוש בשיטת sort() ולאחר מכן שיטת reverseOrder() הופכת את הסדר הטבעי, ואנו מקבלים את המערך הממוין בסדר יורד.

תחביר:

 public static Comparator reverseOrder() 

נניח, a[] הוא מערך שיש למיין בסדר יורד. נשתמש בשיטת reverseOrder() בצורה הבאה:

 Arrays.sort(a, Collections.reverseOrder()); 

בואו נמיין מערך מחרוזות בסדר יורד.

מחרוזת למספר שלם java

SortStringArrayExample3.java

 import java.util.*; public class SortStringArrayExample1 { public static void main(String args[]) { //defining an array of type String String[] countries = {&apos;Zimbabwe&apos;, &apos;South-Africa&apos;, &apos;India&apos;, &apos;America&apos;, &apos;Yugoslavia&apos;, &apos; Australia&apos;, &apos;Denmark&apos;, &apos;France&apos;, &apos;Netherlands&apos;, &apos;Italy&apos;, &apos;Germany&apos;}; //sorts array in descending order Arrays.sort(countries, Collections.reverseOrder()); //prints the sorted string array in descending order System.out.println(Arrays.toString(countries)); } } 

תְפוּקָה:

 [Zimbabwe, Yugoslavia, South-Africa, Netherlands, Italy, India, Germany, France, Denmark, America, Australia]