Jump to content
Unity Insider Forum
Sign in to follow this  
Elharter

List Sort/Compare-To Problem

Recommended Posts

Hallo,

ich hänge, kann mir bitte jemand weiter helfen:

 

 public void UpdateQualifiyingScoreBoard()
    {
        QualiPanelText.text = "Online-Players:"+cntOnlinePlayersLoop+"\n";
        foreach (Player p in PhotonNetwork.PlayerList)
        {
            if (qBestLapOrderList.Count < PhotonNetwork.PlayerList.Length)
            { 
                qBestLapOrderList.Add(new qBestLapOrder(p.NickName, p.GetBestLap()));
                qBestLapOrderList.Sort(SortByBestLap);

            }
            for (int cnt = 0; cnt < qBestLapOrderList.Count; cnt++)
            {
                qBestLapOrderList[cnt].qBestLap = p.GetBestLap();

                float tmpbestlap = p.GetBestLap();
                int minutes_b = Mathf.FloorToInt(tmpbestlap / 60F);
                int seconds_b = Mathf.FloorToInt(tmpbestlap - minutes_b * 60);
                int milliseconds_b = Mathf.FloorToInt((tmpbestlap - seconds_b) * 100);
                string niceTime_b = string.Format("{0:0}:{1:00}:{2:00}", minutes_b, seconds_b, milliseconds_b);

                QualiPanelText.text += qBestLapOrderList[cnt].qNickname + ":" + niceTime_b;
            }
        }

    }

    public static int SortByBestLap(qBestLapOrder p1, qBestLapOrder p2)
    {
        return p1.qBestLap.CompareTo(p2.qBestLap);
    }

 

Ich raff die CompareTo Methode nicht ganz..... ich will 1-12 Spieler sortieren. 
Kurz erklärt:

1,2,3 oder 12 Spieler fahren ein Qualifiying. Ich sende über CustomProperties die Bestlap und verarbeite diese. Somit verfüge ich zentral über die BestLap jedes gejointen Fahrers.
Nun möchte ich diese Liste sortieren. Schnellster ganz oben, langsamster ganz unten.

Passiert aber nicht. Was mache ich falsch?

danke 

Share this post


Link to post
Share on other sites

behoben

 

 public void UpdateQualifiyingScoreBoard()
    {
        QualiPanelText.text = "Quali-Drivers: "+cntOnlinePlayersLoop+"\n";
        foreach (Player p in PhotonNetwork.PlayerList)
        {
            //QualiPanelText.text += "\n" + p.NickName+":"+p.GetBestLap();
            // Insert Listentrys the first time
            if (qBestLapOrderList.Count < PhotonNetwork.PlayerList.Length)
            {
                 qBestLapOrderList.Add(new qBestLapOrder(p.NickName, p.GetBestLap()));
               }
            for (int cnt = 0; cnt < qBestLapOrderList.Count; cnt++)
            {
                // Update List Entry everytime after first insert
                if(qBestLapOrderList[cnt].qNickname == p.NickName)
                {
                    qBestLapOrderList[cnt].qBestLap = p.GetBestLap();
                }
               
            }
            //qBestLapOrderList = qBestLapOrderList.OrderBy(qBestLapOrderList.).ToList();
            //qBestLapOrderList.Sort(SortByBestLap);
            qBestLapOrderList.Sort((l1, l2) => l1.qBestLap.CompareTo(l2.qBestLap));

        }
        for (int cnt = 0; cnt < qBestLapOrderList.Count; cnt++)
        {
            float tmpbestlap = qBestLapOrderList[cnt].qBestLap;
            int minutes_b = Mathf.FloorToInt(tmpbestlap / 60F);
            int seconds_b = Mathf.FloorToInt(tmpbestlap - minutes_b * 60);
            int milliseconds_b = Mathf.FloorToInt((tmpbestlap - seconds_b) * 100);
            string niceTime_b = string.Format("{0:0}:{1:00}:{2:00}", minutes_b, seconds_b, milliseconds_b);

            if (qBestLapOrderList[cnt].qBestLap > 1)
            {
                QualiPanelText.text += "P" + (cnt + 1) + "." + qBestLapOrderList[cnt].qNickname + "(" + niceTime_b + ")\n";
            }
        }

    }

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...