java - Collections.sort & Bubble sort -


iam using 2 different ways of sorting results in arraylist. first way collections.sort works fine no problem there. other sorting algoritm bubblesort (i know ineffective, using in study purposes) have collections sort sort results biggest values @ 0 , second biggest @ 1 etc. want bubble sort algoritm sort other way around, smallet values @ 0 etc. mentioned, collections sort works fine bubble sort doesnt sort them way want to.

is method or loop wrong?

private void sort(boolean resultcomparison, arraylist<result> list) {     if (resultcomparison= false) {         boolean moved;         {             moved= false;             (int = 1; < list.size(); i++) {                 result in = list.get(i - 1);                 result de = list.get(i);                 if (in.value() < de.value()) {                     list.set(i - 1, de);                     list.set(i, in);                     moved= true;                 }             }         } while (moved);     } else {         collections.sort(list);     } } 

the boolean resultcomparison attribute each instance of class has, false should print out results small big.

currently, bubble sort sorting in descending order, largest value @ 0 index in list. because swapping 2 consecutive values if first 1 less second, e.g. [4, 5] => 4 < 5 => [5, 4].

reverse comparison, swap 2 consecutive values if first 1 greater second. change

if (in.value() < de.value()) { 

to

if (in.value() > de.value()) { 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -