none
How can I sort Stack of my customized class? RRS feed

  • 질문

  •  Hello, I have been struggling with sorting Stack of My customized class.  I have researched how to sort Stack in google, so I've gotten some way to do that like added temp stack or Linq method. 

    but What I really got confused with is that the Stack what I want sort is in my customized class. It's gotten me kind of headache.

    My codes are below

     
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    
    namespace My_Practice
    {
        class Student
        {
            Stack<Int32> Grades = new Stack<Int32>();
            
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public int Age { get; set; }
            public string Program { get; set; }
    
            public Student(string first, string last, int age, string prog)
            {
                this.FirstName = first;
                this.LastName = last;
                this.Age = age;
                this.Program = prog;
            }
    
            public void Testscores(int score)
            {    
                Grades.Push(score);       
            }
    
            public void ChangingGrades (int change_score)
            {          
                Grades.Pop();    
                Grades.Push(change_score);
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace My_practice
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Create a Generic List of type Student
                List<Student> students = new List<Student>();
    
                Student stud1 = new Student("Tom", "Thumb", 12, "Computer Science");
                Student stud2 = new Student("Fred", "Flintstone", 45, "Geology");
                Student stud3 = new Student("Mickey", "Mouse", 35, "Animation");
    
    
                //stud1_Grades_Stack
                stud1.Testscores(10);
                stud1.Testscores(20);
                stud1.Testscores(30);
                stud1.Testscores(40);
                stud1.Testscores(50);
    
                //stud2_Grades_Stack
                stud2.Testscores(29);
                stud2.Testscores(10);
                stud2.Testscores(7);
                stud2.Testscores(0);
                stud2.Testscores(1);
    
                //stud3_Grades_Stack
                stud3.Testscores(99);
                stud3.Testscores(92);
                stud3.Testscores(2);
                stud3.Testscores(72);
                stud3.Testscores(87);
    
                // Remove the last entered number and replace it with new number
                stud1.ChangingGrades(7);
                stud2.ChangingGrades(18);
                stud3.ChangingGrades(100);
    
                // Add Student's instance to List
                students.Add(stud1);
                students.Add(stud2);
                students.Add(stud3);
    
                foreach (Student stud in students)
                {
                    Console.WriteLine($"Students FirstName: {stud.FirstName} and LastName: {stud.LastName}");
                }          
            }
        }
    }

    this is my all codes.  

    The reason why I keep getting confused is I feel like Stack instance Grades is separated with Student instances i.e: stud1, stud2 and stud3. My codes are storing 5 int data type in Stack instance (Grades) in Student class using Testscores method. but I have no Idea, where and how I can sort Stack instances of each Student objects.  

    what I want to do is sort Stack of each of stud instance and display on console with WriteLine statement.

    Does anyone give me any idea?

    cheers. 


    • 편집됨 Exclusive_pro 2018년 8월 15일 수요일 오전 11:54
    2018년 8월 15일 수요일 오전 11:45

모든 응답

  • 스택은 원래부터 LIFO 자료 구조이기 때문에 정렬에 대한 예비가 없는 자료 구조입니다.

    스택을 정렬하고 싶다면, Stack의 ToArray 메서드를 호출해 배열로 받아온 다음 그것을 정렬해서 출력하면 됩니다.

    2018년 8월 16일 목요일 오전 12:45