0

Alright so this is my first question here at stack. So i have the current code (Note im using c#)-

   public static void ReadSuburbs()
        {
            String directory = @"C:\Address Sorting\";
            String[] linesA = File.ReadAllLines(Path.Combine(directory, "FileA-Database.txt"));
            String[] linesB = File.ReadAllLines(Path.Combine(directory, "Suburbs.txt"));

            IEnumerable<String> onlyB = linesB.Intersect(linesA);

            File.WriteAllLines(Path.Combine(directory, "ResultsSuburbs.txt"), onlyB);
        }

What im trying to do is get all the suburbs from database a and print them exactly as they were in the text file. So ill just give you a example of a result i was looking to get.

Lets say my database a contained the street names

12 margret st kallangur

14 simpson st zillmere

43 pauls rd bowen hills

And the suburbs.txt contains a list of all suburbs in australia

The result im looking for would be

kallangur

zillmere

bowen hills

But what im getting is

Bowen HIlls
Kallangur
Zillmere

Is there another method i can use except intersect to get the result im looking for?

EDIT : So basically what i have is a database full of addresses that have been inputted incorrectly - So basically we have 12 saint street Kallangur and many other addresses in this database and what i need to do is seperate the suburbs from the street names.

  • 1
    I don't see how you are getting any output at all, since `12 margret st kallangur` and from the other file just `kallangur` are not the same string. In your example, not a single line from FileA-Database.txt matches a single line from Suburbs.txt exactly. – Eric J. Feb 18 '16 at 23:28
  • .Intersect() should not change the order `the marked elements are yielded in the order in which they were collected.` see the Remarks section https://msdn.microsoft.com/library/bb460136%28v=vs.100%29.aspx?f=255&MSPPError=-2147217396 – Eric J. Feb 18 '16 at 23:31
  • Are the suburbs in Suburbs.txt alphabetically sorted? That might be the case. – Rahul Feb 18 '16 at 23:33
  • Yeah they dont match exactly, but i have been getting output, is there a different method i should use? And Rahul yeah they are alphabetically sorted, took me a while to gather all the suburbs in australia and i sorted it alphabetically. – Dillon Munien Feb 18 '16 at 23:37
  • Then as @nineberry said, the order is determined by your sorted list. Change your intersect query per his recommendation – Rahul Feb 18 '16 at 23:41
  • We'd still need a bit more details. Your task might not be so easy to solve because there are actually valid road names containing suburb names. – NineBerry Feb 19 '16 at 00:11
  • Not sure what other details to give – Dillon Munien Feb 19 '16 at 00:14

1 Answers1

1

If you have

IEnumerable<String> onlyB = linesB.Intersect(linesA);

the contents of linesB define the order of the resulting sequence. If you want linesA to define the order, just turn the statement around and use

IEnumerable<String> onlyB = linesA.Intersect(linesB);
NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • Thank you! That fixed my problem :) Also i understand what i did wrong. Haha woops – Dillon Munien Feb 18 '16 at 23:40
  • You should be aware that if you have multiple addresses in the same suburb, you will only get one entry in the resulting sequence per suburb! – NineBerry Feb 18 '16 at 23:43
  • Ahh just realised that just then when i was going through the file, rather not be spoon fed so, can i have a hint to fix that? – Dillon Munien Feb 18 '16 at 23:48
  • It is difficult to know what you are actually trying to achieve. Edit your question and explain a bit more detailled what you are actually trying to do. – NineBerry Feb 18 '16 at 23:51
  • Edited - EDIT : So basically what i have is a database full of addresses that have been inputted incorrectly - So basically we have **12 saint street Kallangur** and many other addresses in this database and what i need to do is seperate the suburbs from the street names. – Dillon Munien Feb 18 '16 at 23:55
  • Edit again, just used the where function instead, now produces duplicates aswell :) – Dillon Munien Feb 19 '16 at 00:10