sorting array of struct

hey why code is not able to sort on the basis of “e” when the inputs are : 100 4,4 5,200 6,2 5

//JUST PRACTISING

#include<cstdio>
#include<iostream>
#include<list>
#include<vector>
#include<utility>
using namespace std;
struct node
{
    int u;
	int w;
}road[10];

int comp(node a,node b)
{
	if(a.u<b.u)
	return a.u;
	else
	return b.u;

}

int main()
{
	for(int i=0;i<4;i++)
	{  int a,b;
		scanf("%d%d",&road[i].u,&road[i].w);
	}

	 sort(road,road+4,comp);
 printf("\n\n\n\n");
	for(int i=0;i<4;i++)
	{
		printf("%d   %d\n",road[i].u,road[i].w);
	}
	
	system("pause");
}

this is ur corrected code…LINK…the error was in ur comp fxn…also u need to include algorithm header file for using sort fxn…also for sorting in desc order just swap the return values in the comp fxn…hope this helps…:slight_smile:

1 Like

thnx was grt help…:slight_smile: