# csvsort2_many.pl     
# 内容 :　大量のCSVファイルをソートする
# 　　　　文字列型の昇順・降順、数値型の昇順・降順の並べ替えを行う。
# 　　　　(通貨記号や","つきの数字は数値型としての並べ替えはできない)
# Copyright (c) 2011 Mitsuo Minagawa, All rights reserved.
# (minagawa@fb3.so-net.ne.jp)
# 使用方法 : c:\>perl csvsort2_many.pl
#
open(IN1,"input.txt");
open(OUT1,">output.txt");

# 分割してソートするレコードを入れる配列
@in1_rec01	=	();
@in1_rec02	=	();
@in1_rec03	=	();
@in1_rec04	=	();
@in1_rec05	=	();
@in1_rec06	=	();
@in1_rec07	=	();
@in1_rec08	=	();
@in1_rec09	=	();
@in1_rec10	=	();
# 分割してソートするレコードの件数
$in1_ctr01	=	0;
$in1_ctr02	=	0;
$in1_ctr03	=	0;
$in1_ctr04	=	0;
$in1_ctr05	=	0;
$in1_ctr06	=	0;
$in1_ctr07	=	0;
$in1_ctr08	=	0;
$in1_ctr09	=	0;
$in1_ctr10	=	0;

# CSVファイルの入力
while	($line1	=	<IN1>)	{

	chomp($line1);
#	$tmp	=	$line1;	
#	$tmp	=~	s/(?:\x0D\x0A|[\x0D\x0A])?$/,/;
#	@in1	=	map {/^"(.*)"$/ ? scalar($_ = $1, s/""/"/g, $_) : $_}
#               ($tmp =~ /("[^"]*(?:""[^"]*)*"|[^,]*),/g);
	@in1	=	split("\t",$line1,-1);

	if		($in1[1]	le		"01203")				{
			$in1_rec01[$in1_ctr01]	=	join("\t",@in1);
			$in1_ctr01++;
	}
	elsif	($in1[1]	le		"01210")				{
			$in1_rec02[$in1_ctr02]	=	join("\t",@in1);
			$in1_ctr02++;
	}
	elsif	($in1[1]	le		"01223")				{
			$in1_rec03[$in1_ctr03]	=	join("\t",@in1);
			$in1_ctr03++;
	}
	elsif	($in1[1]	le		"01346")				{
			$in1_rec04[$in1_ctr04]	=	join("\t",@in1);
			$in1_ctr04++;
	}
	elsif	($in1[1]	le		"01453")				{
			$in1_rec05[$in1_ctr05]	=	join("\t",@in1);
			$in1_ctr05++;
	}
	elsif	($in1[1]	le		"01547")				{
			$in1_rec06[$in1_ctr06]	=	join("\t",@in1);
			$in1_ctr06++;
	}
	elsif	($in1[1]	le		"01636")				{
			$in1_rec07[$in1_ctr07]	=	join("\t",@in1);
			$in1_ctr07++;
	}
	elsif	($in1[1]	le		"01691")				{
			$in1_rec08[$in1_ctr08]	=	join("\t",@in1);
			$in1_ctr08++;
	}
	elsif	($in1[1]	le		"02205")				{
			$in1_rec09[$in1_ctr09]	=	join("\t",@in1);
			$in1_ctr09++;
	}
	elsif	($in1[1]	le		"02401")				{
			$in1_rec10[$in1_ctr10]	=	join("\t",@in1);
			$in1_ctr10++;
	}
}

# CSVファイルの並べ替え
# 文字列タイプの昇順:$a[1]	cmp	$b[1] #2番目の項目の昇順
# 文字列タイプの降順:$b[0]	cmp	$a[0] #1番目の項目の降順
# 数値タイプの昇順:$a[1]	<=>	$b[1] #2番目の項目の昇順
# 数値タイプの降順:$b[0]	<=>	$a[0] #1番目の項目の降順
#
	@sort_rec01	=	sort	{	
								my @a	=	split("\t", $a);
								my @b	=	split("\t", $b);
								$a[1]	<=>	$b[1]
							or	$a[0]	cmp	$b[0]
							or	$a[3]	cmp	$b[3]
							or	$a[2]	cmp	$b[2];
					}
					@in1_rec01;

	@sort_rec02	=	sort	{	
								my @a	=	split("\t", $a);
								my @b	=	split("\t", $b);
								$a[1]	<=>	$b[1]
							or	$a[0]	cmp	$b[0]
							or	$a[3]	cmp	$b[3]
							or	$a[2]	cmp	$b[2];
					}
					@in1_rec02;

	@sort_rec03	=	sort	{	
								my @a	=	split("\t", $a);
								my @b	=	split("\t", $b);
								$a[1]	<=>	$b[1]
							or	$a[0]	cmp	$b[0]
							or	$a[3]	cmp	$b[3]
							or	$a[2]	cmp	$b[2];
					}
					@in1_rec03;

	@sort_rec04	=	sort	{	
								my @a	=	split("\t", $a);
								my @b	=	split("\t", $b);
								$a[1]	<=>	$b[1]
							or	$a[0]	cmp	$b[0]
							or	$a[3]	cmp	$b[3]
							or	$a[2]	cmp	$b[2];
					}
					@in1_rec04;

	@sort_rec05	=	sort	{	
								my @a	=	split("\t", $a);
								my @b	=	split("\t", $b);
								$a[1]	<=>	$b[1]
							or	$a[0]	cmp	$b[0]
							or	$a[3]	cmp	$b[3]
							or	$a[2]	cmp	$b[2];
					}
					@in1_rec05;

	@sort_rec06	=	sort	{	
								my @a	=	split("\t", $a);
								my @b	=	split("\t", $b);
								$a[1]	<=>	$b[1]
							or	$a[0]	cmp	$b[0]
							or	$a[3]	cmp	$b[3]
							or	$a[2]	cmp	$b[2];
					}
					@in1_rec06;

	@sort_rec07	=	sort	{	
								my @a	=	split("\t", $a);
								my @b	=	split("\t", $b);
								$a[1]	<=>	$b[1]
							or	$a[0]	cmp	$b[0]
							or	$a[3]	cmp	$b[3]
							or	$a[2]	cmp	$b[2];
					}
					@in1_rec07;

	@sort_rec08	=	sort	{	
								my @a	=	split("\t", $a);
								my @b	=	split("\t", $b);
								$a[1]	<=>	$b[1]
							or	$a[0]	cmp	$b[0]
							or	$a[3]	cmp	$b[3]
							or	$a[2]	cmp	$b[2];
					}
					@in1_rec08;

	@sort_rec09	=	sort	{	
								my @a	=	split("\t", $a);
								my @b	=	split("\t", $b);
								$a[1]	<=>	$b[1]
							or	$a[0]	cmp	$b[0]
							or	$a[3]	cmp	$b[3]
							or	$a[2]	cmp	$b[2];
					}
					@in1_rec09;

	@sort_rec10	=	sort	{	
								my @a	=	split("\t", $a);
								my @b	=	split("\t", $b);
								$a[1]	<=>	$b[1]
							or	$a[0]	cmp	$b[0]
							or	$a[3]	cmp	$b[3]
							or	$a[2]	cmp	$b[2];
					}
					@in1_rec10;

# CSVファイルの出力

foreach	$record	(@sort_rec01)	{
	print	OUT1	"$record\n";
}
foreach	$record	(@sort_rec02)	{
	print	OUT1	"$record\n";
}
foreach	$record	(@sort_rec03)	{
	print	OUT1	"$record\n";
}
foreach	$record	(@sort_rec04)	{
	print	OUT1	"$record\n";
}
foreach	$record	(@sort_rec05)	{
	print	OUT1	"$record\n";
}
foreach	$record	(@sort_rec06)	{
	print	OUT1	"$record\n";
}
foreach	$record	(@sort_rec07)	{
	print	OUT1	"$record\n";
}
foreach	$record	(@sort_rec08)	{
	print	OUT1	"$record\n";
}
foreach	$record	(@sort_rec09)	{
	print	OUT1	"$record\n";
}
foreach	$record	(@sort_rec10)	{
	print	OUT1	"$record\n";
}

close(IN1);
close(OUT1);
