You are not logged in. Please login at to post your questions!


Retrieving y co-ordinates

Hi friends, I recently came up with a question. Kindly tell me it's solution. My question:

There are n points with co-ordinates (x1,y1), (x2,y2),........, (xn,yn). I want to sort these points based on the value of their x co-ordinates. Therefore I am using the sort function to sort the x co-ordinates, but am unable to access there respective y co-ordinates. Please tell me a way by which I can retrieve the corresponding y co-ordinates after sorting with the best efficiency.

Thanks in advance

asked 26 Jan '15, 10:03

anupam_datta's gravatar image

accept rate: 7%

sort using struct,bool func.

      struct vert

         int xcord,ycord;

    bool cmp (vert a,vert b) //two objects a&b of type vert(struct)


        return a.xcord<b.xcord;//sorting structs based on xcord


//in main function

   vert v[n];//assume n pts are there


answered 26 Jan '15, 10:49

a1a99_3's gravatar image

accept rate: 16%


@a1a99_3 I carried out your answer and am getting the desired answer. Thanks! However, can you kindly tell me why/how is the 'cmp' function sorting the structure based on the xcord and how is it being carried out in the sort function.Thanks onc e again!

(26 Jan '15, 13:07) anupam_datta4★

@anupam_datta...<personal>...Didn't I explain it to you a billion times in school!?!?! And HOW DO YOU carry out AN !?Answer?! and get the "DESIRED" !?Answer?!...LOL (CaRrY oUt???)

I am the one who liked ur SILLY comment and am very sure that I'll remain to ONLY one to do so...HA HA HA!!!

(26 Jan '15, 23:55) arpanb83★

i bet u r solving RECTANGLES..

(27 Jan '15, 00:02) arpanb83★

OOOOOOOOOOOOOOOOOOOOOO...u got another like?!?!?!?!

(28 Jan '15, 15:09) arpanb83★

@arpanb8 Therefore aren't you SILLY enough to consider my comment SILLY?!?!?!?!?!

(29 Jan '15, 21:34) anupam_datta4★

i mean 2 is the upper bound for numberOfLikes. numberOfLikes<=2...

(29 Jan '15, 23:39) arpanb83★
showing 5 of 6 show all

You can also use a vector of pairs. Try running the below code to understand how.

Personally, I am biased towards this method of sorting as I've never used structs before :P


answered 26 Jan '15, 14:52

sandy999's gravatar image

accept rate: 10%

edited 26 Jan '15, 14:54


Or an array of pairs.

(26 Jan '15, 15:02) superty3★
toggle preview

Follow this question

By Email:

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



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "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
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 26 Jan '15, 10:03

question was seen: 890 times

last updated: 29 Jan '15, 23:39

Related questions