import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.regex.Pattern;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Main {
public static class Record {
private int mLineNum;
private double mValue;
public Record
( int aLineNum,
String aWord,
double aValue
){ this.mLineNum = aLineNum;
this.mWord = aWord;
this.mValue = aValue;
}
public String getWord
() { return mWord
; } public int getLineNum() { return mLineNum; }
public double getValue() { return mValue; }
@Override
return this.mWord;
}
}
public static abstract class RecordSplitter {
int mLineNumber;
{
mLineNumber = 0;
}
Record record;
do {
line = mBufferedReader.readLine();
record = this.createRecord(mLineNumber++,line);
} while( line != null && record == null );
if( line == null ){
mBufferedReader.close();
mInputStreamReader.close();
mFileInputStream.close();
mLineNumber = -1;
}
return record;
}
public abstract Record createRecord
( int aLineNum,
String aLine
);
}
static public String DefaultFileName
= "data.txt"; static public String DefaultEncode
= "JISAutoDetect";
RecordSplitter rs = new RecordSplitter(
aArgs.length > 0 ? aArgs[0] : DefaultFileName,
aArgs.length > 1 ? aArgs[1] : DefaultEncode
) {
@Override
public Record createRecord
( int aLineNum,
String aLine
){ if( aLine == null || ! Pattern.matches("^.+\\s[0-9]+(\\.[0-9]+)?$",aLine) ){
return null;
}
String[] kv
= aLine.
split("\\s"); return new Record
( aLineNum, kv
[0],
Double.
parseDouble(kv
[1]) ); }
};
List<Record> list = new ArrayList<Record>();
for( Record r = rs.next(); r != null; r = rs.next() ){
list.add(r);
}
Collections.
sort( list,
new java.
util.
Comparator<Record
>(){ @Override
public int compare( Record aObjLhs, Record aObjRhs ) {
Record lhs = (Record) aObjLhs;
Record rhs = (Record) aObjRhs;
double sub = lhs.getValue() - rhs.getValue();
if( sub == 0 ){ sub = lhs.getLineNum() - rhs.getLineNum(); }
return -( sub < 0.0 ? -1 : (sub > 0.0 ? 1 : 0) );
}
});
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CmltcG9ydCBqYXZhLnV0aWwuTGlzdDsKa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