Sorting in java

Java Discuss, Sorting in java at Programmers Lounge forum; hi... can anybody please help me how to sort numbers in java?? you know, smallest to biggest numbers?? and vise ...


Go Back   Gamerz Needs - For All Your Gaming Needs! > Technology Zone > Programmers Lounge > Java
Forgot Password? | Sign Up!

Notices

Advertisement
   

Reply
 
Bookmark this Thread Tools Display Modes
  #1  
Old 11-08-2007, 04:24 AM
dirky00018's Avatar
Violet Hole
 
Last Online: 11-19-2008 08:41 AM
Join Date: Mar 2007
Posts: 352
Thanks: 42
Thanked 18 Times in 12 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Latest Blog:
Rep Power: 5
dirky00018 is on a distinguished road
Points: 738.80
Bank: 3,730.62
Total Points: 4,469.42
Dark Blue - dirky00018 Dark Blue - dirky00018 Dark Blue - dirky00018 
Question Sorting in java

hi... can anybody please help me how to sort numbers in java?? you know, smallest to biggest numbers?? and vise versa...
  #2  
Old 11-08-2007, 06:00 AM
Stone Axe
 
Last Online: Yesterday 10:14 AM
Join Date: Oct 2006
Location: Jerusalem the holy city
Posts: 42
Thanks: 9
Thanked 2 Times in 2 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Latest Blog:
Rep Power: 0
garbett1 is on a distinguished road
Points: 1,516.02
Bank: 684.64
Total Points: 2,200.66
Send a message via Skype™ to garbett1
well your best bet is to make a tree set then make a comparable method to sort then
for the comparable use:

public interface Comparable<int>{
public int compareTo(int a);
}

then for the class

public class anyClass implements Comparable<int>{
int[] myInts = (//add all the ints you want to sort here);
public int compareTo(int a){
return (this - int); //you can choose whatever you want
}
public static void main(String[] args) {
Collection myCollection = new TreeSet()<int>;
for(int int1 : myInts){
myCollection.add(int1);
}for(int int2 : myCollection){
System.out.println(int2);
}
}
}
that should work if not just tweak the compare to method till it does if the int doesnt work just change the <int> to <Integer> what u want hope that helped also look at:
Java 2 Platform SE v1.3.1: Interface Comparable

Last edited by garbett1; 11-08-2007 at 06:29 AM.. Reason: method isnt static
The Following User Says Thank You to garbett1 For This Useful Post:
dirky00018 (11-08-2007)
  #3  
Old 11-08-2007, 07:49 AM
dirky00018's Avatar
Violet Hole
 
Last Online: 11-19-2008 08:41 AM
Join Date: Mar 2007
Posts: 352
Thanks: 42
Thanked 18 Times in 12 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Latest Blog:
Rep Power: 5
dirky00018 is on a distinguished road
Points: 738.80
Bank: 3,730.62
Total Points: 4,469.42
Dark Blue - dirky00018 Dark Blue - dirky00018 Dark Blue - dirky00018 
uhmm.. sorry if im noob but i really dont understand your solution.. im still a begginer with java.. i still dont know what interface is.. let me just show you the problem. its simple for advance programmers..

problem:
make a program that will ask the user to enter 5 numbers. store it in a array and output the ff:
1.) maximum number
2.) minimum number
3.) sorted array

i already got the minimum and maximum. my problem is the sorting... hope someone can help..
  #4  
Old 11-08-2007, 09:29 AM
Stone Axe
 
Last Online: Yesterday 10:14 AM
Join Date: Oct 2006
Location: Jerusalem the holy city
Posts: 42
Thanks: 9
Thanked 2 Times in 2 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Latest Blog:
Rep Power: 0
garbett1 is on a distinguished road
Points: 1,516.02
Bank: 684.64
Total Points: 2,200.66
Send a message via Skype™ to garbett1
hmmm well look on wikipedia about bubble sort that will probably help you if you cant figure it out pm me
-----------------This post was merged together. In future use the edit button.-----------------

Last edited by garbett1; 11-08-2007 at 09:36 AM.. Reason: bad idea
  #5  
Old 11-11-2007, 05:33 AM
dirky00018's Avatar
Violet Hole
 
Last Online: 11-19-2008 08:41 AM
Join Date: Mar 2007
Posts: 352
Thanks: 42
Thanked 18 Times in 12 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Latest Blog:
Rep Power: 5
dirky00018 is on a distinguished road
Points: 738.80
Bank: 3,730.62
Total Points: 4,469.42
Dark Blue - dirky00018 Dark Blue - dirky00018 Dark Blue - dirky00018 
BTW, i got the solution.. i used bubble sorting..
thanks for the help though..

im posting the solution.. hope this would help noobs like me in the future..



Quote:
Originally Posted by finding maximum and minimum value in an array and sorting
public static void main(String[] args) {
BufferedReader a = new BufferedReader (new InputStreamReader(System.in));
String temp = "";
int size = 5;
int num[] = new int[size];
int max=0;
int min;
int t=0;


System.out.println("Enter 5 numbers: ");

for(int i=0; i<size; i++)
{
try
{
temp=a.readLine();
}
catch(IOException a1){}
try
{
num[i] = Integer.parseInt(temp);
}
catch(NumberFormatException a1)
{
System.out.println("numbers only!!");
}
}

//maximum
for(int i=0; i < size; i++)
{
if(num[i] > max)
max = num[i];
}
System.out.println("The maximum number is: " + max);



//minimum
min = num[0];
for(int i=0; i < size; i++)
{
if (num[i] < min)
min = num[i];
}
System.out.println("The minimum number is: " + min);




//sorting

for (int x=1; x <size; x++)
for(int b=size-1; b>=x; b--)
{
if(num[b-1]>num[b]) //if out of order
{
t=num[b-1]; //exchange of numbers
num[b-1]=num[b];
num[b]=t;
}
}


System.out.println("The sorted array is: ");
for(int i=0; i<size;i++)
{
System.out.println(num[i]);
}
}
}
hope this helps others..

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Advertisement
   


Main Navigation
Home
GzN Forums
GzN Games
GzN News
Top Games
GzN Cheats
GzN Articles
GzN Reviews
GzN Downloads
User Control Panel
Advertising
RSS Feed
2Moons
Adventure Quest
AirRivals
America's Army
Anarchy Online
Archlord
Audition
Battlefield Series
Cabal Online
Call Of Duty Series
Combat Arms
Conquer Online
Counter Strike
Day of Defeat
Deicide Online
Diablo Series
Doom Series
Drift City
Enemy Territory
Eudemons Online
Final Fantasy
Flyff (Fly For Fun)
General Game Discussion
Ghost Online
Granado Espada
Grand Theft Auto Series
Guild Wars
Gunbound
Gunz Online
Habbo Hotel
Half-Life 2
Hero Online
KartRider
Knights Online
Maple Story
Medal of Honor
MU Online
Neopets
Pangya
Quake Series
Ragnarok Online
Rappelz
Rakion
Red Orchestra
Rose Online
Runescape
Scions of Fate
Silkroad Online
Sims Series
Soldier Front
Starcraft
Tales of Pirates
Tibia
The Ship
Trickster Online
TS Online
Unreal Tournament
War Rock
WolfTeam
World of Warcraft & Series
Affiliates
COD4 Hacks
BF2 Hacks


All times are GMT -8. The time now is 04:37 PM.