Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How would I rearrange the lines in the file:
prop1:aaa
-
prop3:ccc
prop2:bbb
-


to match the order shown in the listbox:
Index	PropertyName	DefaultValue
0		prop1			aaa
1		-				-
2		prop2			bbb
3		-				-
4		prop3			ccc


?
Posted
Updated 25-Oct-15 11:11am
v4
Comments
Maciej Los 25-Oct-15 13:45pm    
What have you tried till now?
Where are you stuck?
coder2134 25-Oct-15 13:51pm    
I just have no idea where to begin. I know I'd probably need to iterate through each item in the listbox but I have no clue how to rearrange the lines in the file to match the correct order
coder2134 25-Oct-15 13:56pm    
What I mean is that it's not going to be difficult but I don't know how to approach it
Maciej Los 25-Oct-15 14:35pm    
You have to improve a question. Till now we we know nothing about Listbox datasource.
Sergey Alexandrovich Kryukov 25-Oct-15 14:48pm    
It's really bad to show images (bitmaps) to conduct purely textual information.
—SA

coder2134[^] wrote:
How would I rearrange the lines in the file (...) to match the order (...)?


If you want to compare two list, you can achieve that by using Linq[^]. So, there's no need to rearrange anything.

VB
'you should load data into variables by using File.ReadAllLines method (see solution 1)
Dim lines1 As String() = {"prop1:aaa", "-", "prop3:ccc", "prop2:bbb", "-"}
Dim lines2 As String() = {"prop1:aaa", "-", "prop2:bbb", "-", "prop3:ccc"}

Dim orderedlist1 = lines1 _
	.Where(Function(s) s.Contains(":")) _
	.Select(Function(s) New With _
	{ _
		Key .PropertyName = s.Split(":")(0), _
		Key .DefaultValue = s.Split(":")(1) _
	}) _
	.OrderBy(Function(item) item.PropertyName) _
	.ToList()
	
Dim orderedlist2 = lines2 _
	.Where(Function(s) s.Contains(":")) _
	.Select(Function(s) New With _
	{ _
		Key .PropertyName = s.Split(":")(0), _
		Key .DefaultValue = s.Split(":")(1) _
	}) _
	.OrderBy(Function(item) item.PropertyName) _
	.ToList()


Result (orderedlist1 and orderedlist2 are exactly the same):
PropertyName DefaultValue
prop1        aaa 
prop2        bbb 
prop3        ccc 


To compare them, use Enumerable.Except method[^]:
VB
Dim result = orderedlist1.Except(orderedlist2).Count()
'returns the number of differences
'in this case zero (0)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Oct-15 16:00pm    
Yes, this is yet another thing one could do; a 5.
—SA
Maciej Los 25-Oct-15 16:03pm    
Thank you, Sergey.
coder2134 25-Oct-15 17:10pm    
I'm sorting the file in VB and reading it later on in Java.
I need to make sure it's organised in the correct order
Sergey Alexandrovich Kryukov 25-Oct-15 17:24pm    
This is not a question. If you want to make something sure, make it sure.
—SA
coder2134 26-Oct-15 9:13am    
Thanks for the effort, but I'm getting:
Unable to cast object of type 'System.Collections.Generic.List`1[VB$AnonymousType_0`2[System.String,System.String]]' to type 'System.Collections.Generic.IEnumerable`1[System.String]'. on the line:

Dim orderedlist2 = ...
Basically, you read your file into some collection (for example, using System.IO.File.ReadAllLines, manipulate this collection and read the file back.

But I doubt you really need it. If you already read the file, maybe you should better use it in memory. Also, using just an unstructured text file is both lo-tech and too much work on your side. You could have better maintainability and utilize available .NET FCL features better if you use, say, XML or JSON.

—SA
 
Share this answer
 
Comments
Maciej Los 25-Oct-15 15:36pm    
+5!
Sergey Alexandrovich Kryukov 25-Oct-15 15:59pm    
Thank you, Maciej.
—SA
coder2134 25-Oct-15 17:11pm    
I know I'd have to read it into a collection; what I don't know how to do is manipulate it. I don't know how to go about it.
Sergey Alexandrovich Kryukov 25-Oct-15 17:23pm    
All your "I don't know" suggests that you are not qualified for getting answers to your questions. It's very hard to help anyone who always responds "I don't know". It's pretty much like asking "how to do programming?" so the real answer will be "learn some programming". Solution 2 gives you some ideas on manipulation collections. Simpler yet, once you use some collection type, you can always load an MSDN page on that collection type and see what it can do. But if your want to get passive position and repeat your "I don't know" without decent effort, you would be unqualified for profession (or hobby activity alike).
—SA
coder2134 26-Oct-15 9:04am    
The only reason why I'd ever use this site is if I really am stuck and have tried everything else I could think of. All I've received is negativity and, again, 1 star votes on my question as if I could provide more information than I did. Perhaps you should help people instead of being picky on how they word their questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900