sort object array with 2 strings in JAVA
Page 1 of 1
Paintface




Posts: 6877

PostPosted: Thu, 3rd Dec 2015 15:52    Post subject: sort object array with 2 strings in JAVA
I have an object array with 2 Strings each. Each object has one String with a zip code and second String has matching city.
Now i have to sort the objects by their intrernal zipcode String from small to big.
This has to be done in JAVA. thanks!.
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Thu, 3rd Dec 2015 16:22    Post subject:
Can you copy&paste what you have here?

I just need some clarification.

You have an ARRAY with OBJECTS, where EACH OBJECT has 2 properties that store strings. One property is the zip code, the other one is the city name.

Now you would like to sort the ARRAY depending on the zip codes of each OBJECT, right?

In any case you'll need to write your own comperator, then simply run a sort on your object and giving it the new comperator.

I really love javascript. There the solution would look like this

Code:
//create empty array
var myarray = [];

//adding some cities with their zipcode
myarray.push({zipcode:"12345", name:"Something"});
myarray.push({zipcode:"62345", name:"Darkside"});
myarray.push({zipcode:"22345", name:"Something"});

//sort with custom function on zipcode from small to big
myarray.sort(function(a,b){return a.zipcode>b.zipcode});

//output string
myarray.map(function(e){return e.name}).join(" ")


And if desired in a nicer way with proper class creation Smile
 Spoiler:
 


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
Werelds
Special Little Man



Posts: 15098
Location: 0100111001001100
PostPosted: Thu, 3rd Dec 2015 16:51    Post subject:
Rusty, but something akin to this:

Code:
public class ZipcodeComparator implements Comparator<ZipCity> {
    @Override
    public int compare(ZipCity lhs, ZipCity rhs) {
        return lhs.getZipcode().compareTo(rhs.getZipcode());
    }
}


And then:

Code:
Collections.sort(zipcities, new ZipcodeComparator());


Where ZipCity is your object class, zipcities is a Collection of ZipCity.

Should work. You can just rely on the innate string comparing functionality, so all you gotta do is use that string comparison for your object comparison as well.
Back to top
Paintface




Posts: 6877

PostPosted: Fri, 4th Dec 2015 00:50    Post subject:
Code:
public class Zipcity {

   private String zipcode;
   private String city;

   public Postnummer(String zipcode, String city) {

      this.zipcode = zipcode;
      this.city = city;

   }


Code:
public class Zipcitycollection {

   private Zipcity[] zipcitycollection = new Zipcity[5];


Code:

public class Main {

   public static void main(String[] args) {

      Zipcitycollection collection = new Zipcitycollection();

      collection.add("1264", "Random5");
      collection.add("4587", "Random2");
      collection.add("6844", "Random1");
      collection.add("6548", "Random6");
      collection.add("9847", "Random7l");

}
}


I found when googling alot of info about sorting arraylists, and shorter ways of doing it in JAVA 8 which i use. But its a array filled with objects and i need to sort them once by each field. Never did that before so thanks if you guys can help me out.
Back to top
Paintface




Posts: 6877

PostPosted: Fri, 4th Dec 2015 02:03    Post subject:
finally got it working.....

thanks for the input guys
Back to top
madmax17




Posts: 17725
Location: Croatia
PostPosted: Fri, 27th May 2016 22:00    Post subject:
Sounds like a job for a 'map', it has a key and a value.
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group