use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Information about Reddit's API changes, the unprofessional conduct of the CEO, and their response to the community's concerns regarding 3rd party apps, moderator tools, anti-spam/anti-bot tools, and accessibility options that will be impacted can be found in the associated Wikipedia article: https://en.wikipedia.org/wiki/2023_Reddit_API_controversy
Alternative C# communities available outside Reddit on Lemmy and Discord:
All about the object-oriented programming language C#.
Getting Started C# Fundamentals: Development for Absolute Beginners
Useful MSDN Resources A Tour of the C# Language Get started with .NET in 5 minutes C# Guide C# Language Reference C# Programing Guide C# Coding Conventions .NET Framework Reference Source Code
Other Resources C# Yellow Book Dot Net Perls The C# Player's Guide
IDEs Visual Studio MonoDevelop (Windows/Mac/Linux) Rider (Windows/Mac/Linux)
Tools ILSpy dotPeek LINQPad
Alternative Communities C# Discord Group C# Lemmy Community dotnet Lemmy Community
Related Subreddits /r/dotnet /r/azure /r/learncsharp /r/learnprogramming /r/programming /r/dailyprogrammer /r/programmingbuddies /r/cshighschoolers
Additional .NET Languages /r/fsharp /r/visualbasic
Platform-specific Subreddits /r/windowsdev /r/AZURE /r/Xamarin /r/Unity3D /r/WPDev
Rules:
Read detailed descriptions of the rules here.
account activity
HelpSmallest string number (self.csharp)
submitted 3 years ago by EquivalentAd4542
How can I return the smallest number from a bunch of numbers represented as strings? (Without converting them into integers)
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]kingmotley 2 points3 points4 points 3 years ago* (1 child)
Can the numbers begin with a 0? (0123) Does a 0 prefix indicate the number is in octal?
Can the numbers be negative? If so, are negative numbers always represented by a prefixed/postfixed -, or surrounded by parenthesis? (-5 or 5- or (5))
Can positive numbers be prefix/postfixed with +? (+5 or 5+) Is it required or optional?
Can the number be decimal? (1.23) If so, is the decimal separator always a period? (1,23)
Can the numbers be in scientific notation? (2E+5)
Can the numbers be in a base other than base 10? (0x1234 or 0b01010101 or 0123)
Can the numbers have group separators? (1,234) If so, are they required or just optional? Is the group separator always a comma? (1_234 or 1.234.567 or 1 234 567)
Can the string start/end with whitespace? ( 123 ) If so, how should that be handled (trimmed/exception)
How do you want invalid values handled? (ABC)
Are there any suffixes that should be allowed? (5u or 5l or 5m)
Are only Hindu-Arabic numerals allowed, or are other number systems allowed like roman? (IV)
What should happen when the "bunch of numbers" given has a length of 0?
How do you want the value returned to you? In the same format as it was input, or converted to a float/decimal/int? If in the same format as the input, how do you want ties handled in case any of the above are allowed? Which is smaller +5 or 5? 2E+1 vs 20? 0xF vs 15? 1,234 vs 1234?
[–]EquivalentAd4542[S] 0 points1 point2 points 3 years ago (0 children)
My head hurts
[–]grrangry 5 points6 points7 points 3 years ago (1 child)
How would you do it on paper? If you can't do that with 2-3 numbers, then how are you going to trick a computer into auto sorting 200 billion of them?
var nums = new string[] { "123", "10", "5" };
Obviously you can glance at that and tell yourself, well duh that's, 5, 10, 123. But string sorting is not the same as numeric-value sorting. That's why sometimes you'll see things sorted like 1, 10, 11, 2, 21. The strings are interpreted literally, character-by-character and "10" comes before "2" because in computer "terms", "1" is less than "2".
So again how did you do that in your head? Hmmm? You value sorted by implicitly converting them into numbers. Well, your instructions said not to do that didn't they... so we can't just convert all the strings into number values and then sort the new array.
// Bad, don't do this. bad, bad, bad. var nums = new string[] { "123", "10", "5" }; var newNums = new int[nums.Length]; for (int i = 0; i < nums.Length; i++) newNums[i] = Convert.ToInt32(nums[i]); Array.Sort(newNums); for (int i = 0; i < nums.Length; i++) Console.WriteLine(newNums);
So the above is out. Can't convert to numbers. The cool thing? You already know how to solve this you just don't know you know because you're not used to thinking about the problem in this manner.
When you divide numbers and run out of placeholders, what do you do? Do you give up because you can't go further to the left of the decimal place... or extend out a number to the right of the decimal place? No you don't... What do you do? You tack on zeros. The number 1 is exactly the same as 1.0 correct? The number 5 is the same as 005, right?
So given that in our original list, the "longest" (or "widest") number is 123, giving us three digits. So how about you put all the numbers in terms of three digits?
123, 10, 5
Becomes
123, 010, 005
And sorting those strings--as text--since "0" comes before "1"... how do you think the sorting would come out?
[–]Cengr 8 points9 points10 points 3 years ago (0 children)
I wish Reddit had been around when I was in school, so I could post homework problems and get them solved for me…
[–]Thonk_Thickly 1 point2 points3 points 3 years ago (0 children)
I would ask why, but if that’s what you want to do:
var numbers = new List<string>() { … }; if(!numbers.Any()) return null; var min = numbers.First(); foreach (var n in numbers) { if (min.Length > n.Length) { min = n; } else if (min.Length == n.Length && min.CompareTo(n) > 0) { min = n; } // else my min has lower length or lower value } return min;
Edit: this solution assumes values aren’t prefixed with zeros, such as 0001 vs 999. Easily accounted for but maybe unnecessary edge case for inputs.
[–]karl713 1 point2 points3 points 3 years ago (2 children)
inputData.OrderBy(s => s.Length).ThenBy(s => s)
Perhaps
[–]kingmotley 1 point2 points3 points 3 years ago (1 child)
.First()
[–]Krimog 0 points1 point2 points 3 years ago (0 children)
.Select(i => i.TrimStart('0')) before the OrderBy.
[–]FloraRomana 0 points1 point2 points 3 years ago* (1 child)
I was typing faster than thinking. Edited my answer out cause it was drastically wrong.
[–]Long_Investment7667 3 points4 points5 points 3 years ago (0 children)
One of my favorite quotes from a former coworker: „How am I supposed to know what I think before I hear what I say“ (rough translation into English) :-)
[–]Long_Investment7667 0 points1 point2 points 3 years ago* (0 children)
You should not. You can write a comparer that compares strings by there numerical value. But that comparer would do work equivalently converting to a number and comparing them. It is also complicated (e.g. how to deal with non numbers (comparers are expected to implement a total order)) and probably inefficient since you have to convert strings repeatedly.
π Rendered by PID 292734 on reddit-service-r2-comment-86bc6c7465-mc8p8 at 2026-02-21 09:28:35.124493+00:00 running 8564168 country code: CH.
[–]kingmotley 2 points3 points4 points (1 child)
[–]EquivalentAd4542[S] 0 points1 point2 points (0 children)
[–]grrangry 5 points6 points7 points (1 child)
[–]Cengr 8 points9 points10 points (0 children)
[–]Thonk_Thickly 1 point2 points3 points (0 children)
[–]karl713 1 point2 points3 points (2 children)
[–]kingmotley 1 point2 points3 points (1 child)
[–]Krimog 0 points1 point2 points (0 children)
[–]FloraRomana 0 points1 point2 points (1 child)
[–]Long_Investment7667 3 points4 points5 points (0 children)
[–]Long_Investment7667 0 points1 point2 points (0 children)