Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

Sort array list

Sort array list alphabetically.

How do i sort arraylist alphavetically, i am using compareTo method but only first two elements are being sorted.
0
zizi21
Asked:
zizi21
  • 3
  • 3
  • 2
  • +3
2 Solutions
 
entceeCommented:
myarray.sort()
0
 
entceeCommented:
Sorry, that was javascript, here is java:

Arrays.sort(myArray);
0
 
entceeCommented:
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ksivananthCommented:
how do you sort? can you post the code?
0
 
Manikandan ThiagarajanSenior consultantCommented:
0
 
Manikandan ThiagarajanSenior consultantCommented:
0
 
Manikandan ThiagarajanSenior consultantCommented:
0
 
willyRDCommented:
Hello!

I add an example hoping it helps understanding how Arrays.sort() works. If your ArrayList contains an Object different from String you'll have to create a Comparator to tell the sort method how to sort two objects in your ArrayList.



package test;

import java.util.ArrayList;
import java.util.Arrays;

public class sortArrays {

      public static void main(String[] args) {
            ArrayList list = new ArrayList();
            
            //add Strings to ArrayList
            list.add(new String("juliet"));
            list.add(new String("zulu"));
            list.add(new String("foxtrot"));
            list.add(new String("alpha"));
            
            System.out.println("Before sorting: "+list.toString());      
            Object[] listToSort = list.toArray();
            System.out.println(listToSort[0].getClass().getName());
            
            Arrays.sort(listToSort);      //listToSort is now sorted
            
            //copy elements to ArrayList
            list.clear();
            for(int i=0; i<listToSort.length; i++) {
                  list.add(listToSort[i]);
            }
            
            System.out.println("After sorting: "+list.toString());
      }
}
0
 
zizi21Author Commented:
my apologies for this. just saw this. was away.
0
 
zizi21Author Commented:
hi, i am trying to sort objects...

ArrayList <House> h=new ArrayList<House>();
ArrayList <House> tmp=new ArrayList<House>();
n=0;
for(i=0;i<h.size();i++)
{
for(j=0;j<h.size();j++)
{
if(h.get(i).getAdd().compareTo(h.get(j).getAdd())<0)
{
                        
tmp.add(h.get(i));
tmp.set(n,h.get(i));
stu.set(i, h.get(j));
stu.set(j,tmp.get(i));
                        
}
                  }
            
0
 
CEHJCommented:
class House implements Comparable<House> {
      private String address;

      public void setAddress(String address) {
            this.address = address;
      }

      public String getAddress() {
            return address;
      }

      public int compareTo(House o) {
            return address.compareTo(o.address);
      }
..........
}

You can then do

ArrayList <House> h = new ArrayList <House>();
// Fill it then
Collections.sort(h);
0
 
CEHJCommented:
:-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now