+ 20

HOW To SORT elements of string array (ascending) ?

example ::: String abc ={"acid","abed","eds"}; // then i want to get elements sorted like String sorted={"abed","acid","eds"}; //is there any method for that ... or i hv to write code for that too 😐 if u know plzz tell ... thanks 4 the help ☺☺

19th Sep 2017, 11:48 AM
Changed
Changed - avatar
31 Answers
+ 10
thanks all for the gr8 responce to my doubt ☺☺☺ these is a challenge for u ... try it //hope u will find it interesting https://www.sololearn.com/Discuss/727976/?ref=app
20th Sep 2017, 8:13 AM
Changed
Changed - avatar
19th Sep 2017, 1:14 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 11
java only @kartikey
19th Sep 2017, 12:02 PM
Changed
Changed - avatar
+ 9
Collections.sort(abc);
19th Sep 2017, 3:42 PM
Vukan
Vukan - avatar
+ 9
use the sort method of d Arrays class, for example: String [] arr= {"aer","err"}; Arrays.sort(arr);
21st Sep 2017, 9:00 AM
David Akhihiero
David Akhihiero - avatar
+ 8
thank u all friends , i got the method which I wanted ... its Arrays.sort (<arrayname>); thanks all once again ☺☺
19th Sep 2017, 4:03 PM
Changed
Changed - avatar
+ 6
Bookmarked!
21st Sep 2017, 7:22 AM
Ipang
+ 5
Python? or only Java?
19th Sep 2017, 11:59 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 5
check below link this is the best program I found: after copying code from website, replace old string with String str={"sggdh","ghhdhx"}; https://javasimplify.blogspot.in/2017/09/java-program-to-sort-string-without.html?m=1
19th Sep 2017, 3:27 PM
Rushikesh Chaudhari
Rushikesh Chaudhari - avatar
+ 4
Bubble sort
19th Sep 2017, 3:00 PM
Sandeep mishra
Sandeep mishra - avatar
+ 4
Do like this- String str [] ={"afsf","hsja","hdjs"};//unsorted array List<String> list=Arrays.asList(str);//array to list Collections.sort(list);//sorted list str= list.toArray(new String[0]);// sorted array Intermediate conversation to list is required as Collections.sort is not applicable to array
19th Sep 2017, 7:40 PM
Debapriya Sen
Debapriya Sen - avatar
+ 4
import java.util.ArrayList; import java.util.Collections; ArrayList<Character> arr = new ArrayList<Character>; push elements into it and then use: Collections.sort(arr); Instead of character you can substitute any data type.
20th Sep 2017, 10:08 AM
Haris
Haris - avatar
+ 4
bubblesort
21st Sep 2017, 12:44 PM
meru agostinho daudo meru
+ 3
Your question suppose you can sort an array of integers or doubles. I will not give you a code because i don't know your level for choosing a sorting algorithm but I can say you this: generally languages provide string comparison you can test the language in which you want to implement the sorting algorithm and test something like this a = "abc" b = "efg" c = a > b a and b are both strings c is a Boolean print the value of c if it's correct (c is false because lexicographically a is less than b) you can write the same algorithm that you use for sorting an integers or doubles and applied that to strings. I have tried to make explanation simple as possible, sorry for my bad English 😅.
19th Sep 2017, 5:11 PM
Koumaré Abdoulaye
Koumaré Abdoulaye - avatar
+ 3
import java.util.Arrays and use Arrays. sort(yourArray) it is a static method and can sort any array of any datatype
21st Sep 2017, 5:28 PM
Aman Mehra
Aman Mehra - avatar
+ 3
import java.util.Arrays; import java.io.*; /*How many names you want to sort : 5 Enter 1 name : Mark Enter 2 name : Bill Enter 3 name : Larry Enter 4 name : John Enter 5 name : Alexander Sorted names -> Alexander Bill John Larry Mark*/ class SortString { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // create a Java String array String[] names = null; System.out.print("How many names you want to sort : "); int size = Integer.parseInt(br.readLine()); names = new String[size]; for(int i = 0; i<size;i++) { System.out.print("Enter "+(i+1)+" name : "); names[i] = br.readLine(); } // sort the array, using the sort method of the Arrays class Arrays.sort(names); System.out.println("Sorted names -> "); // print the sorted results for (String name : names) { System.out.println("\t"+name); } } }
22nd Sep 2017, 5:23 AM
Yatin Nayyar
Yatin Nayyar - avatar
+ 2
without using api or methods..javasimplify code is best
19th Sep 2017, 3:44 PM
Rushikesh Chaudhari
Rushikesh Chaudhari - avatar
+ 2
I don't know
19th Sep 2017, 5:32 PM
Rayan the coder
Rayan the coder - avatar
+ 2
alguna programadora
19th Sep 2017, 7:21 PM
cristian