মনে করুন, আমার এই রকম একটা স্ট্রাকচার (Structure) আছে—

struct Student  { 
    string name; 
    int id;
    int age;
    string department;
};

Student a[3];

// Student 1 
a[0].name = "Mehadi" ; 
a[0].id = 10; 
a[0].age = 25 ; 
a[0].department = "CSE" ;

// Student 2
a[0].name = "Hasan" ; 
a[0].id = 5; 
a[0].age = 24 ; 
a[0].department = "EEE" ;

// Student 3
a[0].name = "Menon" ; 
a[0].id = 100; 
a[0].age = 30 ; 
a[0].department = "ETE" ;

এই স্ট্রাকচারকে আমি age অনুসারে সর্ট করতে চাই। কীভাবে করা যেতে পারে?

asked 04 Feb, 05:21

menon's gravatar image

menon
4.7k335

edited 04 Feb, 10:24

Mosharraf%20Hosain's gravatar image

Mosharraf Hosain ♦
74618


আশা করি আপনি কোড দেখে বুঝতে পারবেন।

#include <bits/stdc++.h>

using namespace std;

struct Student  {
    string name;
    int id;
    int age;
    string department;
};

bool compare(Student a, Student b) {
    return a.age < b.age;
}

int main()
{

    Student a[3];

    // Student 1
    a[0].name = "Mehadi";
    a[0].id = 10;
    a[0].age = 25;
    a[0].department = "CSE";

    // Student 2
    a[1].name = "Hasan";
    a[1].id = 5;
    a[1].age = 24 ;
    a[1].department = "EEE";

    // Student 3
    a[2].name = "Menon";
    a[2].id = 100;
    a[2].age = 30;
    a[2].department = "ETE";

    sort(a, a + 3, compare);

    for(int i = 0; i < 3; i++) {
        cout << a[i].name << " " << a[i].id << " " << a[i].age << " " << a[i].department << endl;
    }


    return 0;
}
permanent link

answered 05 Feb, 14:22

sourav_hossain's gravatar image

sourav_hossain
1548

edited 06 Feb, 16:16

Mosharraf%20Hosain's gravatar image

Mosharraf Hosain ♦
74618

1

@sourav_hossain কোডটা আমি বুঝতে পারছি । তারপরও bool compare মেথড এবং sort(a, a + 3, compare) এই জায়গা টা একটু ব্যখ্যা করলে ভালো হতো । তাহলে অন্য অনেকের জন্য এটা বুঝতে আরও সহজ হতো ।

(06 Feb, 02:13) menon
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×402
×131
×13
×11

question asked: 04 Feb, 05:21

question was seen: 752 times

last updated: 06 Feb, 16:16