+ 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 ☺☺
31 Respuestas
+ 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
+ 18
you can sort like this too
https://code.sololearn.com/cdDYwlr48uUJ/?ref=app
+ 11
java only @kartikey
+ 9
Collections.sort(abc);
+ 9
use the sort method of d Arrays class, for example:
String [] arr= {"aer","err"};
Arrays.sort(arr);
+ 8
thank u all friends ,
i got the method which I wanted ... its Arrays.sort (<arrayname>);
thanks all once again ☺☺
+ 6
Bookmarked!
+ 5
Python? or only Java?
+ 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
+ 4
Bubble sort
+ 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
+ 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.
+ 4
bubblesort
+ 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 😅.
+ 3
import java.util.Arrays
and use
Arrays. sort(yourArray)
it is a static method and can sort any array of any datatype
+ 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);
}
}
}
+ 2
without using api or methods..javasimplify code is best
+ 2
I don't know
+ 2
alguna programadora