Problem:

Sorting Pairs

 

  Write a program which print coordinates $(x_i, y_i)$ of given $n$ points on the plane by the following criteria.

first by $x$-coordinate

in case of a tie, by $y$-coordinate

Input

 

  The input is given in the following format.

$n$
$x_0 \; y_0$
$x_1 \; y_1$
:
$x_{n-1} \; y_{n-1}$

  In the first line, the number of points $n$ is given. In the following $n$ lines, coordinates of each point are given.

Output

  Print coordinate of given points in order.

Constraints

$1 \leq n \leq 100,000$

$-1,000,000,000 \leq x_i, y_i \leq 1,000,000,000$

Sample Input 1

5
4 7
5 5
2 3
6 8
2 1

Sample Output 1

2 1
2 3
4 7
5 5
6 8



Sample input:
5
4 7
5 5
2 3
6 8
2 1


Sample output:
2 1
2 3
4 7
5 5
6 8

Submission status: Accepted

Solution code:
n = int(input())
points = []
for i in range(n):
    x, y = list(map(int, input().split(' ')))
    points.append([x, y])

points = sorted(points, key=lambda x: x[1])
points = sorted(points, key=lambda x: x[0])

for x, y in points:
    print(x, y)

Submission status: Accepted

Solution code:
# coding=utf-8
N = int(input())

A = [list(map(int, input().split())) for i in range(N)]

A.sort()

[print(' '.join(map(str, x))) for x in A]

Submission status: Accepted

Solution code:

from collections import namedtuple


Point = namedtuple('Point', ['x', 'y'])


def sort(li):
    """Sort a list of Point(x, y) ascending order

    >>> sort([Point(x=2, y=1), Point(x=1, y=0)])
    [Point(x=1, y=0), Point(x=2, y=1)]
    >>> sort([Point(x=1, y=4), Point(x=1, y=3)])
    [Point(x=1, y=3), Point(x=1, y=4)]
    """
    li = sorted(li, key=lambda p: p.y)
    return sorted(li, key=lambda p: p.x)


def run():
    n = int(input())
    ps = []
    for _ in range(n):
        x, y = [int(i) for i in input().split()]
        ps.append(Point(x, y))

    for p in sort(ps):
        print("{} {}".format(p.x, p.y))


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
# AOJ ITP2_5_A: Sorting Pairs
# Python3 2018.6.24 bal4u

ps = []
n = int(input())
for i in range(n):
	x, y = map(int, input().split())
	ps.append((x, y))
a = sorted(ps, key=lambda x:(x[0], x[1]))
for i in range(n): print(*a[i])

Submission status: Accepted

Solution code:
# AOJ ITP2_5_A: Sorting Pairs
# Python3 2018.6.24 bal4u

ps = []
n = int(input())
for i in range(n):
	x, y = map(int, input().split())
	ps.append((x, y))
ps.sort()
for i in ps: print(*i)

Submission status: Accepted

Solution code:
n=int(input())
s=[]
for i in range(n):
    x,y=map(int,input().split())
    s.append((x,y))
s.sort()
for i in range(n):
    print(s[i][0],s[i][1])

Submission status: Accepted

Solution code:
readline = open(0).readline
N = int(readline())

P = [tuple(map(int, readline().split())) for i in range(N)]
P.sort()
open(1, 'w').writelines(["%d %d\n" % e for e in P])

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    xxyy = [list(map(int, input().split())) for _ in range(n)]
    xxyy = sorted(xxyy, key=lambda x: x[1])
    xxyy = sorted(xxyy, key=lambda x: x[0])
    for xy in xxyy:
        print(*xy)


resolve()

Submission status: Accepted

Solution code:
n = int(input())
A = list([0, 0] for i in range(n))
for i in range(n) :
    A[i][0], A[i][1] = map(int, input().split())
A.sort()
for i in range(n) :
    print(A[i][0], A[i][1])

Submission status: Accepted

Solution code:

from sys import stdin

n = int(stdin.readline())
l = []
for i in range(n):
    a,b = map(int,stdin.readline().split())
    l.append([a,b])

l.sort()

for i in range(n):
    print(f'{l[i][0]} {l[i][1]}')

Submission status: Accepted

Solution code:
a = []
for i in range(int(input())):
    a.append(list(map(int,input().split())))
a.sort()
for i,j in a:
    print(i,j)

Submission status: Accepted

Solution code:
n = int(input())
points = []
for i in range(n):
    points.append(list(map(int, input().split())))
points.sort()
for i in range(n):
    print(*points[i])

Submission status: Accepted

Solution code:
ps = []
n = int(input())
for i in range(n):
    x,y = map(int,input().split())
    ps.append((x,y))
ps.sort()
for i in ps:
    print(*i)

Submission status: Accepted

Solution code:
n = int(input())
l = []
for i in range(n):
    l.append(list(map(int, input().split())))
l.sort()
for i in range(n):
    print(*l[i])

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A = []
    for _ in range(n):
        a,b = map(int,input().split())
        A.append([a,b])
    A.sort()
    for a,b in A:print(a,b)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
P = [tuple(map(int,input().split())) for _ in range(N)]

P.sort()

for p in P:
    print(*p)

Submission status: Accepted

Solution code:
n = int(input())

L = []
for i in range(n):
    L.append(list(map(int, input().split())))

L = sorted(L)

[print(*L[i]) for i in range(len(L))]

Submission status: Accepted

Solution code:
n = int(input())
v = []
[v.append(list(map(int, input().split()))) for _ in range(n)]
[print(" ".join(map(str,a))) for a in sorted(v)]

Submission status: Accepted

Solution code:
class Point():
    def __init__(self, x, y):
        self.x = x
        self.y = y

def main():
    n = int(input())
    Points = list()

    for _ in range(n):
        x, y = map(int, input().split())
        p = Point(x, y)
        Points.append(p)

    Points.sort(key=lambda x : x.y)
    Points.sort(key=lambda x : x.x)

    for p in Points:
        print(str(p.x) + " " + str(p.y))

main()

Submission status: Accepted

Solution code:
b = int(input())
a = sorted([list(map(int,input().split())) for i in range(b)])
for c in a:
    print(c[0],c[1])

Submission status: Accepted

Solution code:
n=int(input())

pairs=[]

for _ in range(n):
    xy=[int(i) for i in input().split(" ")]
    pairs.append(xy)

pairs=sorted(pairs)

for p in pairs:
    print(p[0],p[1])

Submission status: Accepted

Solution code:
n = int(input())
a = []
for _ in range(n):
    # x = list(map(int,input().split()))
    x = tuple(map(int,input().split()))
    a.append(x)

for i in sorted(a):
    print(*i)

Submission status: Accepted

Solution code:
pairs=[]
n=int(input())
for i in range(n):
	x,y=map(int, input().split())
	pairs.append((x,y))
pairs.sort()

for i in pairs:
    print(*i)

Submission status: Accepted

Solution code:
def solve():
    from sys import stdin
    f_i = stdin
    
    n = int(f_i.readline())
    
    c = []
    for i in range(n):
        c.append(tuple(map(int, f_i.readline().split())))
    
    c.sort()
    
    ans = (' '.join(map(str, c_i)) for c_i in c)
    print('\n'.join(ans))
    
    
solve()

Submission status: Accepted

Solution code:
class Pairs:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __lt__(self, other):
        if self.x < other.x:
            return True
        elif self.x == other.x:
            return self.y < other.y
        else:
            return False

    def __str__(self):
        return "{} {}".format(self.x, self.y)


n = int(input())
L = []
for i in range(n):
    x, y = [int(x) for x in input().split() ]
    L.append(Pairs(x,y))
L.sort()

for i in L:
    print(i)

Submission status: Accepted

Solution code:
from typing import List, Tuple


if __name__ == "__main__":
    n = int(input())
    coordinates: List[Tuple[int, int]] = []

    for _ in range(n):
        (x, y) = tuple(map(lambda x: int(x), input().split()))
        coordinates.append((x, y))

    coordinates = sorted(coordinates, key=lambda x: (x[0], x[1]))

    for elem in coordinates:
        print(f"{elem[0]} {elem[1]}")

Submission status: Accepted

Solution code:
#組み込み関数頼み
n  = int(input())
P = []
for _ in range(n):
    p = list(map(int, input().split( )))
    P.append(p)
P.sort()
for _ in range(n):
    print(*P[_])

Submission status: Accepted

Solution code:
ps = []
n = int(input())
for i in range(n):
	x, y = map(int, input().split())
	ps.append((x, y))
ps.sort()
for i in ps: print(*i)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Permutation - Sorting Pairs
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_5_A&lang=jp

"""
n = int(input())
for p in sorted([[int(a) for a in input().split()] for _ in range(n)]):
    print(*p)

Submission status: Accepted

Solution code:
ps = []
n = int(input())
for i in range(n):
    x, y = map(int, input().split())
    ps.append((x, y))
ps.sort()
for i in ps: print(*i)

Submission status: Accepted

Solution code:
import sys
if __name__ == '__main__':

	n = int(input())

	A = [sys.stdin.readline().split() for _ in range(n)]

	B = sorted(A,key=lambda x:(int(x[0]),int(x[1])))

	for i in range(n):
		print(*B[i])

Submission status: Accepted

Solution code:
n = int(input())

list_2d = [list(map(int, input().split())) for _ in range(n)]


list_2d.sort()

for list_xy in list_2d:
    print(*list_xy)

Submission status: Accepted

Solution code:
n = int(input())
ans = []
for i in range(n):
    x, y = map(int, input().split())
    ans += [(x,y)]
    
ans.sort()
for a in ans:
    print(*a)

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
    x, y = map(int, input().split())
    a.append((x, y))
a.sort()
for i in a: print(*i)

Submission status: Accepted

Solution code:
n=int(input())
A=[]

for i in range(n):
    x=list(map(int,input().split()))
    A.append(x)

A=sorted(A)
for i in range(n):
    print(str(A[i][0])+" "+str(A[i][1]))

Submission status: Accepted

Solution code:
# Sorting Pairs

n = int(input())
P = []

for i in range(n):
    x, y = (int(x) for x in input().split())
    P.append((x, y))

P.sort(key=lambda p: (p[0], p[1]))

for p in P:
    print(p[0], p[1])

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_5_A&lang=jp
# Sorting Pairs

import sys
input = sys.stdin.readline

def main():
    l = []
    for i in range(int(input())):
        (a,b) = map(int,input().split())
        l.append((a,b))
    for a,b in sorted(l):
        print (a,b)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
n = int(input())
pts = [None] * n
for i, line in enumerate(sys.stdin.readlines()):
    x, y = map(int, line.split())
    pts[i] = (x, y)
pts.sort(key=lambda x: (x[0], x[1]))
print('\n'.join(map(lambda x: f'{x[0]} {x[1]}', pts)))

Submission status: Accepted

Solution code:
n = int(input())
mat = []

for i in range(n):
    x, y = map(int, input().split())
    mat.append([x, y])

ans = sorted(mat, key=lambda x: (x[0], x[1]))

for i in range(n):
    print(' '.join(str(m) for m in ans[i]))

Submission status: Accepted

Solution code:
readline = open(0).readline
N = int(readline())
P = [tuple(map(int, readline().split())) for i in range(N)]
P.sort()
open(1, 'w').writelines(["%d %d\n" % e for e in P])

Submission status: Accepted

Solution code:
n = int(input())
li = []

for i in range(n):
    l = list(map(int, input().split()))
    li.append(l)

li = sorted(li)

for i in li:
    print(" ".join(map(str, i)))

Submission status: Accepted

Solution code:
coordinates = [list(map(int, input().split())) for _ in [0]*int(input())]
for c in sorted(coordinates): print(*c)

Submission status: Accepted

Solution code:
n = int(input())
A = []
for i in range(n):
    A.append(list(map(int, input().split())))
A = sorted(A, key = lambda x: (x[0],x[1]))
for i in range(n):
    print(str(A[i][0]) + " " + str(A[i][1]))

Submission status: Accepted

Solution code:
n = int(input())
xy = [list(map(int, input().split())) for i in range(n)]
xy.sort(key=lambda x:x[1])
xy.sort(key=lambda x:x[0])
for ele in xy:
  print(ele[0], ele[1])