troubleshooting Question

Sort list of complex objects alphabetically

Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox asked on
Java
5 Comments1 Solution279 ViewsLast Modified:
Hi,

I need to sort a list of complex objects. Looks like this:

  class Farm {
     String name;
     Data mLotsOfData;
  }

  List<Farm> farms = getAllFarms();

  // Now i want to build a new alphabetical list.
  TreeMap<String, Farm> alpha = new TreeMap<String, Farm>();
  for (all farms) {
      alpha.put(farms.get(i).name, farms.get(i));
  }

  // Back to a list.
  ArrayList<Farm> alphaFarms = new ArrayList<Farm>();
  for (all in alpha) {
      alphaFarms.add(it.getValue());
  }

but it seems to not do anything -what's the right way to do this?

Thanks
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros