Home > Blockchain >  Why is my sort function doing the job incorectly?
Why is my sort function doing the job incorectly?

Time:04-14

Aparently, when i do the sort with only parametrized objects it preforms flawlessly, yet when i add the ones that don't have data, it just goes nuts. Help.

Btw, i want the data in falling order sorted by the "Stanje na racunu", or in english "your account balance", which takes the "stanje" variable in the object class and compares it. I'll repeat once more. This works flawlessly when tr1, tr2 and tr3 are not included. Help

    public class Glavna {
    
    public static void sort(TekuciRacun[] racuni) {
        TekuciRacun heljp = new TekuciRacun();
        
        for ( int i = 0; i < racuni.length - 1; i  )
            for ( int j = 1; j < racuni.length; j  )
                if ( racuni[i].getStanje() > racuni[j].getStanje() ) {
                    heljp = racuni[i];
                    racuni[i] = racuni[j];
                    racuni[j] = heljp;
                }
        
        for ( int i = 0; i < racuni.length; i  )
            System.out.println("\n"   racuni[i].toString());
    } 

    public static void main(String[] args) {

        TekuciRacun ptr1 = new TekuciRacun("Marko Sisovic", 29381024, 20000, 15000, 0, -10000);
        TekuciRacun ptr2 = new TekuciRacun("Petar Gojkovic", 12341234, 25000, 10000, 5000, -15000);
        TekuciRacun ptr3 = new TekuciRacun("Milojko Marivic", 23455423, 30000, 20000, 15000, -30000);
        
        TekuciRacun tr1 = new TekuciRacun();
        TekuciRacun tr2 = new TekuciRacun();
        TekuciRacun tr3 = new TekuciRacun();
        
        TekuciRacun[] racuni = { ptr1, ptr2, ptr3, tr1, tr2, tr3};
        
        sort(racuni);
    }
}

Here's the result.

Ime i prezime vlasnika: Milojko Marivic
Broj racuna: 23455423
Stanje na racunu: -5000 dinara <====this is the data that's used for sorting
Dozvoljen minus: -30000 dinara

Ime i prezime vlasnika: Petar Gojkovic
Broj racuna: 12341234
Stanje na racunu: 10000 dinara <====
Dozvoljen minus: -15000 dinara

Ime i prezime vlasnika: Marko Sisovic
Broj racuna: 29381024
Stanje na racunu: 5000 dinara <====
Dozvoljen minus: -10000 dinara

Ime i prezime vlasnika: null
Broj racuna: 0
Stanje na racunu: 0 dinara 
Dozvoljen minus: 0 dinara

Ime i prezime vlasnika: null
Broj racuna: 0
Stanje na racunu: 0 dinara 
Dozvoljen minus: 0 dinara

Ime i prezime vlasnika: null
Broj racuna: 0
Stanje na racunu: 0 dinara 
Dozvoljen minus: 0 dinara

    public class TekuciRacun implements IRacun{
    private String vlasnik;
    private int brojRacuna;
    private int uplate;
    private int isplate;
    private int kredit;
    private int dozvoljenMinus;
    private int stanje = 0;
    
    TekuciRacun(){
        
    }
    
    TekuciRacun (String vlasnik, int brojRacuna, int uplate, int isplate, int kredit, int dozvoljenMinus){
        super();
        this.vlasnik = vlasnik;
        this.brojRacuna = brojRacuna;
        this.uplate = uplate;
        this.isplate = isplate;
        this.kredit = kredit;
        this.dozvoljenMinus = dozvoljenMinus;
        
        this.stanje = stanje(uplate, isplate, kredit);
    }
    
    @Override
    public int stanje(int uplate, int isplate, int kredit) {
        return stanje = uplate - isplate - kredit;
    }
    
    @Override
    public String toString() {
        return "Ime i prezime vlasnika: "   vlasnik 
                  "\nBroj racuna: "   brojRacuna
                  "\nStanje na racunu: "   stanje(uplate, isplate, kredit)
                  " dinara \nDozvoljen minus: "   dozvoljenMinus
                  " dinara\nKredit: "   kredit;
    }

    public String getVlasnik() {
        return vlasnik;
    }

    public int getBrojRacuna() {
        return brojRacuna;
    }

    public int getUplate() {
        return uplate;
    }

    public int getIsplate() {
        return isplate;
    }

    public int getKredit() {
        return kredit;
    }

    public int getDozvoljenMinus() {
        return dozvoljenMinus;
    }

    public int getStanje() {
        return stanje;
    }

    public void setVlasnik(String vlasnik) {
        this.vlasnik = vlasnik;
    }

    public void setBrojRacuna(int brojRacuna) {
        this.brojRacuna = brojRacuna;
    }

    public void setUplate(int uplate) {
        this.uplate = uplate;
    }

    public void setIsplate(int isplate) {
        this.isplate = isplate;
    }

    public void setKredit(int kredit) {
        this.kredit = kredit;
    }

    public void setDozvoljenMinus(int dozvoljenMinus) {
        this.dozvoljenMinus = dozvoljenMinus;
    }

    public void setStanje(int stanje) {
        this.stanje = stanje;
    }
    
    
    
}

The desired output is (not the full, only the names(vlasnik))

  1. Petar Gojkovic
  2. Marko Sisovic
  3. Milojko Marivic
  4. null
  5. null
  6. null

Edit. After debugging(adding a println between the two for loops) i can see that after the first "i" iteration the result is perfect, but, the loop keeps going, as it should, but starts doing something it shouldn't do. I guess this is out of bounds of my knowledge since i guess this is some compiler "problem".

CodePudding user response:

Your loop logic is incorrect. To do a proper sort, the inside loop (j) has to start from the current i 1 upward. Instead, it starts from 1 every time. When you only have 3 elements, you may not see the problem.

Basically, it compares elements from the area that was already sorted with elements in the unsorted area.

  • Related