Guest User

Sort file without arrays

a guest
Mar 19th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string input = "";  //Gem indholdet her
  14.  
  15.             StreamReader reader = new StreamReader("fil.txt");
  16.             string delimiter = "";  //Adskiller de enkelte records
  17.  
  18.             //Læs filen linje for linje og adskil records
  19.             while (!reader.EndOfStream)
  20.             {
  21.                 input += delimiter + reader.ReadLine();
  22.                 delimiter = "||";
  23.             }
  24.  
  25.             string output = "";     //Gem outputtet her
  26.  
  27.             //Løb indholdet igennem
  28.             while (input.Length > 0)
  29.             {
  30.                 int entryStart = 0;
  31.                 int entryEnd = input.IndexOf("||", entryStart);
  32.  
  33.                 if (entryEnd == -1)
  34.                 {
  35.                     entryEnd = input.Length;
  36.                 }
  37.  
  38.                 //Find den næste record i indholdet
  39.                 string entry = input.Substring(entryStart, entryEnd - entryStart);
  40.  
  41.                 if (output.Length == 0) //Gem den første record
  42.                 {
  43.                     output = entry;
  44.                 }
  45.                 else
  46.                 {
  47.  
  48.                     int lastnameStart = entry.IndexOf(";") + 1;
  49.                     int lastnameEnd = entry.IndexOf(";", lastnameStart);
  50.  
  51.                     //Find efternavnet for nuværende record
  52.                     string lastname = entry.Substring(lastnameStart, lastnameEnd - lastnameStart);
  53.  
  54.                     int outputStart = 0;
  55.                     bool handled = false;   //Indikerer om recorden er håndteret
  56.                     while(outputStart != -1)
  57.                     {
  58.                         bool last = false;  //Indikerer om der skal foretages flere sammenligninger
  59.                         int outputEnd = output.IndexOf("||", outputStart);
  60.  
  61.                         if (outputEnd == -1)
  62.                         {
  63.                             outputEnd = output.Length;
  64.                             last = true;
  65.                         }
  66.  
  67.                         //Find den næste record i outputtet
  68.                         string outputEntry = output.Substring(outputStart, outputEnd - outputStart);
  69.  
  70.                         int outputLastnameStart = outputEntry.IndexOf(";") + 1;
  71.                         int outputLastnameEnd = outputEntry.IndexOf(";", outputLastnameStart);
  72.  
  73.                         //Find efternavnet for nuværende record i outputtet
  74.                         string outputLastname = outputEntry.Substring(outputLastnameStart, outputLastnameEnd - outputLastnameStart);
  75.  
  76.                         //Check om nuværende record skal være før nuværende record i outputtet
  77.                         bool before = lastname.CompareTo(outputLastname) == -1;
  78.                         if (before)
  79.                         {
  80.                             output = output.Insert(outputStart, entry + "||");
  81.                             handled = true;
  82.                         }
  83.  
  84.                         //Stop loopet, hvis sorteret eller ved sidste gennemløb
  85.                         if (handled || last)
  86.                         {
  87.                             break;
  88.                         }
  89.                         outputStart = outputEnd + 2;
  90.                     }
  91.  
  92.                     //Tilføj nuværende record til sidst i outputtet, hvis ingen sortering
  93.                     if (handled == false)
  94.                     {
  95.                         output = output + "||" + entry;
  96.                     }
  97.                 }
  98.  
  99.                 //Fjern nuværende record og adskiller fra inputtet
  100.                 input = input.Replace(entry, "");
  101.                 input = input.TrimStart(new char[] { '|' });
  102.             }
  103.             //Luk input filen
  104.             reader.Close();
  105.  
  106.             //Tilføj records i outputtet til output fil, adskilt af linjeskift
  107.             StreamWriter writer = new StreamWriter("outfil.txt");
  108.             writer.Write(output.Replace("||", "\r\n"));
  109.             //Gem og luk output filen
  110.             writer.Close();
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment