0

Im totaly new to java programing, and I have been stuck with this problem for 2 weeks now! Im not really sure how to even describe the problem, so I've added the whole code basicly. What I want to do is basicly sort the li.Poeng by value.

    String getYear = yearFrom.getSelectedItem().toString();
    String leFile = "http://www.it.hiof.no/~borres/commondata/fotballstatistikk/CSV/data" + getYear + ".txt".trim();

    List<Functions> filData = Functions.setupList(leFile); 


    String fra = yearFrom.getSelectedItem().toString() + ":" + monthFrom.getSelectedItem().toString() + ":" + dayFrom.getSelectedItem().toString();
    String til = yearTo.getSelectedItem().toString() + ":" + monthTo.getSelectedItem().toString() + ":" + dayTo.getSelectedItem().toString();



    String[] fraDat = fra.split(":");

    int fraDag = Integer.parseInt(fraDat[2]);
    int fraMaan = Integer.parseInt(fraDat[1]);
    int fraYear = Integer.parseInt(fraDat[0]);

    int fraDato = fraYear * 10000 + fraMaan * 100 + fraDag; 



    String[] tilDat = til.split(":");

    int tilDag = Integer.parseInt(tilDat[2]);
    int tilMaan = Integer.parseInt(tilDat[1]);
    int tilYear = Integer.parseInt(tilDat[0]);

    int tilDato = tilYear * 10000 + tilMaan * 100 + tilDag; 

    HashMap<String, TabellLinje> tabell = new HashMap<>();


    //----------------------------------------------------------------------------------------------


    for (int i = 0; i < filData.size(); i++) {

        String[] kampdat = filData.get(i).getdato().split(":"); 
        int dag = Integer.parseInt(kampdat[2]);
        int maan = Integer.parseInt(kampdat[1]);
        int year = Integer.parseInt(kampdat[0]);

        int filDato = year * 10000 + maan * 100 + dag;


        if (fraDato < tilDato) {
            if (tilDato >= filDato) {

                // Calculate points

                int hMaal = Integer.parseInt(filData.get(i).gethMaal());
                int bMaal = Integer.parseInt(filData.get(i).getbMaal());

                String hLag = filData.get(i).gethLag();
                String bLag = filData.get(i).getbLag();

                TabellLinje hjemmelag;
                TabellLinje bortelag;


                if (tabell.containsKey(hLag)) 
                {
                    hjemmelag = tabell.get(hLag);

                } else {
                    hjemmelag = new TabellLinje();  
                    hjemmelag.Navn = hLag;
                    tabell.put(hjemmelag.Navn, hjemmelag);
                }

                if (tabell.containsKey(bLag)) {
                    bortelag = tabell.get(bLag);

                } else {
                    bortelag = new TabellLinje();
                    bortelag.Navn = bLag;
                    tabell.put(bortelag.Navn, bortelag);
                }

                if (hMaal > bMaal) {
                    hjemmelag.Poeng += 3;
                    hjemmelag.Matches++;
                    bortelag.Matches++;

                } else if (hMaal == bMaal) {
                    hjemmelag.Poeng++;
                    bortelag.Poeng++;
                    hjemmelag.Matches++;
                    bortelag.Matches++;

                } else if (bMaal > hMaal) {
                    bortelag.Poeng += 3;
                    hjemmelag.Matches++;
                    bortelag.Matches++;

                }

                hjemmelag.GoalsAgainst += bMaal;
                hjemmelag.GoalsFor += hMaal;
                bortelag.GoalsAgainst += hMaal;
                bortelag.GoalsFor += bMaal;

            }
        }
    }

    // Output to GUI
    textArea.removeAll();
    List<TabellLinje> tabellListe = new ArrayList(tabell.values());

    for (int i = 0; i < tabellListe.size(); i++) {       
        TabellLinje li = tabellListe.get(i);
        textArea.add(li.Navn + " " + li.GoalsFor + " " + li.GoalsAgainst + " " + li.Poeng + " " + li.Matches);
    }

}

The code work the way it should, expect i need an additional code to sort the li.Poeng variable wich is an object in the TabellLinje list.

Sorry for horrible description, and for messy code. Gratitude to the white knight who could help me out! :D

1

1 Answer 1

1

You can use a Comparator and the Collections.sort method:

    List<TabellLinje> tabellListe = new ArrayList(tabell.values());
    Collections.sort(tabellListe, new Comparator<TabellLinje>() {
        @Override
        public int compare(TabellLinje o1, TabellLinje o2) {
            return o1.Poeng - o2.Poeng;
        }
    });

So the sort will use the Comparator to decide which value is higher. Sort is stable so same values are left where they are.

Please adhere to java naming conventions however - variables should be in lower case, "poeng" rather than "Poeng". You may want to consider encapsulating your class too.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for your fast reply!! And I've actually tried to use Comperator simular to your example. But I couldnt quite get my head around how to output the return value to the GUI? Like how does the value "inflict" with the for loop of my output code. This program is basicly making a tabell over played matches and calculates points for each team. How do I get the return value of o1.Poeng - o2.Poeng to be dynamic with the GUI output? My english is far from perfect, and I sense it is probebly making it impossible for you guys to understand what im even asking here :P
oh, and my TabellLinje class looks like this: public class TabellLinje { public String Navn; public int Poeng; public int GoalsFor; public int GoalsAgainst; public int Matches; }
This will sort the data in place, i.e. the tabellListe will now be sorted, before your for loop runs and does the display. If the data can change then you need to re sort the data after any change.
aha, well yeh the data will change indeed. The data is mesured by date so if the date changes in the GUI the output is also changed. But since I call upon the whole class now, like with the tabellListe will it then sort all the content of this or only the Poeng part? Pardon me for keep askin bad question, but i have no knowleadge in this area as I've only been programing java for 4 weeks. Thank you again.
The code will sort the whole List by the value of Poeng - the easiest thing is to try it and see what happens and if there are any issues.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.