Jump to content

[Resolvido] Ordenar por ordem decrescente


Ekate
 Share

Recommended Posts

Boas

Tenho uma duvida em que quero ordenar elementos que são inseridos, e à medida que são inseridos quero que sejam comparados e ordenados por ordem decrescente. Tenho este codigo mas nao esta a ordenar de forma alguma, alguma sugestão?

for (int i = 0; i < size; i++) {
           if((adaptee.get(i)).compareTo(elemento) > 0){
               adaptee.set(i, adaptee.get(i + 1));
               if(adaptee.get(0) == adaptee.get(size)){
                   break;
               }
           }
       }
Link to comment
Share on other sites

http://en.wikipedia.org/wiki/Selection_sort

for (int i = 0; i < adaptee.size(); i++) {
 for (int j = i + 1, int min = i; j < adaptee.size(); j++) {
   if (adaptee.get(j).compareTo(adaptee.get(min) < 0) {
     min = j;
   }
 }

 if (min != i) {
   NomeDaClass aux = adaptee.get(min);
   adaptee.set(min, adaptee.get(i));
   adaptee.set(i, aux);
 }
}
IRC : sim, é algo que ainda existe >> #p@p
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.