Creativeeart

Sorting Array

Sep 3rd, 2020
682
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.Linq;
  6.  
  7. public class BookControllerRoom : MonoBehaviour {
  8.    
  9.     public GameObject[] PlaceParents;
  10.  
  11.     public BookLikes[] BookLikes;
  12.  
  13.  
  14.     public int[] ListLikeArrays;
  15.  
  16.     [SerializeField]
  17.     public AllBooks[] AllBooks;
  18.  
  19.     private void Start()
  20.     {
  21.         BookCollectInformation();
  22.         SortArrayHard();
  23.         BookPlacementHard();
  24.     }
  25.  
  26.     public void BookCollectInformation()
  27.     {
  28.         AllBooks = new AllBooks[BookLikes.Length];
  29.         for (int i = 0; i < BookLikes.Length; i++)
  30.         {
  31.             AllBooks[i] = new AllBooks(BookLikes[i].ID, BookLikes[i].gameObject.name, BookLikes[i].CountLikes, BookLikes[i].gameObject);
  32.         }
  33.     }
  34.  
  35.     public void SortArrayHard()
  36.     {
  37.         var count = -1;
  38.         foreach (var x in AllBooks.OrderByDescending(x => x.BookLikes))
  39.         {
  40.             count++;
  41.             AllBooks[count] = x;
  42.         }
  43.     }
  44.  
  45.     public void BookPlacementHard()
  46.     {
  47.         GameObject ins;
  48.         for (int i = 0; i < AllBooks.Length; i++)
  49.         {
  50.             ins = Instantiate(AllBooks[i].BookGameObject, PlaceParents[i].transform);
  51.             ins.transform.localPosition = Vector3.zero;
  52.             ins.transform.localEulerAngles = Vector3.zero;
  53.             ins.tag = "CloneBook";
  54.             if (i >= 8) break;
  55.         }
  56.     }
  57.  
  58. }
  59.  
  60. [System.Serializable]
  61. public struct AllBooks
  62. {
  63.     public int BookID;
  64.     public string BookName;
  65.     public int BookLikes;
  66.     public GameObject BookGameObject;
  67.  
  68.     public AllBooks(int _BookID, string _BookName, int _BookLikes, GameObject _BookGameObject)
  69.     {
  70.         BookID = _BookID;
  71.         BookName = _BookName;
  72.         BookLikes = _BookLikes;
  73.         BookGameObject = _BookGameObject;
  74.     }
  75. }
  76.  
  77.  
RAW Paste Data