Сортировка строк по первому слову в алфавитном порядке
люди добрые помогите пожалуйста
есть программа которая читает из файла и по кнопочками удаляет добавляет и редактирует записи в файле,
и вот когда она читает из файла на страницу, записи надо отсортировать записи по фамилии в алфавитном порядке
помогите пожалуйста реализовать эту сортировку
Код:
<%@page contentType="text/html" pageEncoding="windows-1251"%>
<%@page import="java.io.*, java.util.*, java.text.Collator"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%!
String strConvert(String src, String cp1, String cp2) throws UnsupportedEncodingException
{
byte[] buf=src.getBytes(cp1);
String rez=new String(buf,cp2);
return rez;
}
String showSome(String some)
{
if (some==null)
return "";
else
return "<font face=\"Arial\" size=\"-1\" color=\"blue\" style=\"font-weight:700\">"+some+"</font>";
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<title>Пример веб-приложения на JSP и сервлетах</title>
<script>
function chkRadio()
{
if((document.f.recno==null)||document.f.recno==undefined)
return false;
selflag=document.f.recno.checked?true:false;
for(i=0; i<document.f.recno.length;i++)
{
if(document.f.recno[i].checked)
{
selflag=true;
break;
}
}
return selflag;
}
function chkEdit()
{
if ((document.f.fio.value=="")||(document.f.nz.value=="")||(document.f.mrk.value=="")||(document.f.ind.value==""))
return false;
else
return true;
}
function checkForm(arg)
{
switch (arg)
{
case 'add':
if(!chkEdit())
alert("Заполните все поля!");
else
{
document.f.todo.value="0";
document.f.submit();
}
break;
case 'del':
if(!chkRadio())
alert("Ни одна запись не выбрана!");
else
{
document.f.todo.value="1";
document.f.submit();
}
break;
case 'chg':
if(!chkRadio())
alert("Ни одна запись не выбрана!");
else
if(!chkEdit())
alert("Заполните все поля!");
else
{
document.f.todo.value="2";
document.f.submit();
}
break;
case 'delall':
if(confirm("Удалить ВСЕ записи?"))
{
document.f.todo.value="3";
document.f.submit();
}
}
}
</script>
</head>
<body>
<h2 align="center" style="color:#771111">Ведомость успеваемости по курсу</h2><h1 align="center" style="color:#111177">"Прикладные аспекты повышения качества оказания эффективного содействия тем, кто ничего не делает"</h1>
<form name="f" action="<%=request.getContextPath()+"/filer"%>" method="post">
<table border="1" align="center" cellpadding="3" cellspacing="3" width="75%">
<tr bgcolor="#e0e0e0">
<th>
№ п/п
</th>
<th>
ФИО
</th>
<th>
№ зачетной книжки
</th>
<th>
Оценка
</th>
<th>
Индекс оценки
</th>
<th>
Выбрать запись
</th>
</tr>
<%
String fname="c:\\tmp\\myfile.dat", fio, nz, mrk, ind;
int i=0;
RandomAccessFile f;
BufferedReader br;
try{
f=new RandomAccessFile(fname, "r");
while(f.getFilePointer()<f.length())
{
fio=strConvert(f.readLine(),"cp1252","cp1251");
nz=strConvert(f.readLine(),"cp1252","cp1251");
mrk=strConvert(f.readLine(),"cp1252","cp1251");
ind=strConvert(f.readLine(),"cp1252","cp1251");
i++;
%>
<tr bgcolor="#eeeeee">
<td><%=i%></td>
<td><%=fio%></td>
<td><%=nz%></td>
<td><%=mrk%></td>
<td><%=ind%></td>
<td align="center"><input type="radio" name="recno" value="<%=i%>"></td>
</tr>
<%
}
f.close();
}
catch(Exception e)
{
%>
<tr>
<td colspan="6" align="center"><b>Исключение: <%=e.toString()%></b></td>
</tr>
<%
}
finally
{
}
String[] listUnsort = {"1.jpg", "10.jpg", "100.jpg", "200.jpg", "11.jpg", "110.jpg"};
System.out.println("До сортировки: "+Arrays.toString(listUnsort));
Arrays.sort(listUnsort);
System.out.println("После сортировки: "+Arrays.toString(listUnsort));
%>
</table>
<hr width="70%">
<table width="60%" border="0" align="center" bgcolor="#eeeedd"
style="color: blue;font-style: oblique; border-left: solid olive; border-right: solid olive; border-bottom: solid olive; border-top: solid olive; font-weight: 700">
<tr>
<td>
ФИО:<input type="text" size="35" name="fio">
</td>
<td>
Номер зачетки:<input type="text" size="20" name="nz">
</td>
<td>
Оценка:<input type="text" size="20" name="mrk">
</td>
<td>
Индекс:<input type="text" size="10" name="ind">
</td>
</tr>
</table>
<hr width="60%">
<div align="center" >
<input type="button" value="Добавить запись" onclick="checkForm('add');">
<input type="button" value="Удалить запись" onclick="checkForm('del');">
<input type="button" value="Изменить запись" onclick="checkForm('chg');">
<input type="button" value="Удалить все записи" onclick="checkForm('delall');">
<input type="reset" value="Сброс">
<input type="hidden" name="todo" value="zero">
</div>
</form>
<hr width="50%">
<div align="center"><%=showSome((String)session.getAttribute("status"))%></div>
<div align="right"><a href="<%=request.getContextPath()+"/serverinfo"%>">Посмотреть все заголовки</a></div>
</body>
</html>
Спасибо!
|