<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[version sort - feedback]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich moechte folgendes beispiel implementieren und brauechte mal ein kurzes feedback zu meinem code. das ist jetzt keine production ready code. es geht nur um wie ich den algo implementieren kann...</p>
<p>/*<br />
How would you sort the list of version numbers. For eg: You are given a list A as<br />
[&quot;1.1&quot;,&quot;1.15&quot;,&quot;12.0&quot;,&quot;1.23&quot;,&quot;12.3&quot;]. Now sort this ?<br />
*/</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;sstream&gt;
#include &lt;algorithm&gt;
using namespace std;

void append_zero(std::string &amp;version, int len) {
	for (int i = 0; i &lt; len; i++) {
		version.push_back('0');
	}
}

std::string get_string_front_comma(std::string &amp;str) {
	auto comma_pos = str.find('.');
	return str.substr(0, comma_pos);
}

std::string get_string_after_comma(std::string &amp;str) {
	auto comma_pos = str.find('.');
	return str.substr(comma_pos, str.size() - comma_pos);
}

int string_to_int(std::string &amp;str) {
	int value = 0;
	std::stringstream ss;
	ss &lt;&lt; str;
	ss &gt;&gt; value;
	return value;
}

void add_zero_padding(std::string &amp;str) {
	auto pos_comma = str.find(&quot;.&quot;);
	if (pos_comma == std::string::npos) {
		throw &quot;invalid version number&quot;;
	}
	int precission = str.size() - pos_comma - 1;
	if (precission &lt; 2) {
		append_zero(str, precission);
	}
}

struct compare {
	bool operator()(std::string lhs, std::string rhs) {
		add_zero_padding(lhs);
		add_zero_padding(rhs);

		// compare front comma
		std::string s1 = get_string_front_comma(lhs);
		std::string s2 = get_string_front_comma(rhs);
		int l = string_to_int(s1);
		int r = string_to_int(s2);
		if (l != r) {
			return l &lt; r;
		}

		// compare after comma
		s1 = get_string_after_comma(lhs);
		s2 = get_string_after_comma(rhs);
		l = string_to_int(s1);
		r = string_to_int(s2);
		return l &lt; r;
	}
};

void version_sort(std::vector&lt;string&gt; &amp;vec) {
	std::sort(vec.begin(), vec.end(), compare());
}

int main() {
	// your code goes here

	std::vector&lt;string&gt; versions = {&quot;1.1&quot;,&quot;1.15&quot;,&quot;12.0&quot;,&quot;1.23&quot;,&quot;12.3&quot;};
	version_sort(versions);

	for (auto e : versions) {
		std::cout &lt;&lt; e &lt;&lt; endl;	
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/334800/version-sort-feedback</link><generator>RSS for Node</generator><lastBuildDate>Sat, 25 Apr 2026 06:37:12 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/334800.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 12 Oct 2015 00:31:32 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to version sort - feedback on Mon, 12 Oct 2015 00:31:32 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich moechte folgendes beispiel implementieren und brauechte mal ein kurzes feedback zu meinem code. das ist jetzt keine production ready code. es geht nur um wie ich den algo implementieren kann...</p>
<p>/*<br />
How would you sort the list of version numbers. For eg: You are given a list A as<br />
[&quot;1.1&quot;,&quot;1.15&quot;,&quot;12.0&quot;,&quot;1.23&quot;,&quot;12.3&quot;]. Now sort this ?<br />
*/</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;sstream&gt;
#include &lt;algorithm&gt;
using namespace std;

void append_zero(std::string &amp;version, int len) {
	for (int i = 0; i &lt; len; i++) {
		version.push_back('0');
	}
}

std::string get_string_front_comma(std::string &amp;str) {
	auto comma_pos = str.find('.');
	return str.substr(0, comma_pos);
}

std::string get_string_after_comma(std::string &amp;str) {
	auto comma_pos = str.find('.');
	return str.substr(comma_pos, str.size() - comma_pos);
}

int string_to_int(std::string &amp;str) {
	int value = 0;
	std::stringstream ss;
	ss &lt;&lt; str;
	ss &gt;&gt; value;
	return value;
}

void add_zero_padding(std::string &amp;str) {
	auto pos_comma = str.find(&quot;.&quot;);
	if (pos_comma == std::string::npos) {
		throw &quot;invalid version number&quot;;
	}
	int precission = str.size() - pos_comma - 1;
	if (precission &lt; 2) {
		append_zero(str, precission);
	}
}

struct compare {
	bool operator()(std::string lhs, std::string rhs) {
		add_zero_padding(lhs);
		add_zero_padding(rhs);

		// compare front comma
		std::string s1 = get_string_front_comma(lhs);
		std::string s2 = get_string_front_comma(rhs);
		int l = string_to_int(s1);
		int r = string_to_int(s2);
		if (l != r) {
			return l &lt; r;
		}

		// compare after comma
		s1 = get_string_after_comma(lhs);
		s2 = get_string_after_comma(rhs);
		l = string_to_int(s1);
		r = string_to_int(s2);
		return l &lt; r;
	}
};

void version_sort(std::vector&lt;string&gt; &amp;vec) {
	std::sort(vec.begin(), vec.end(), compare());
}

int main() {
	// your code goes here

	std::vector&lt;string&gt; versions = {&quot;1.1&quot;,&quot;1.15&quot;,&quot;12.0&quot;,&quot;1.23&quot;,&quot;12.3&quot;};
	version_sort(versions);

	for (auto e : versions) {
		std::cout &lt;&lt; e &lt;&lt; endl;	
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2470704</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2470704</guid><dc:creator><![CDATA[coderatte]]></dc:creator><pubDate>Mon, 12 Oct 2015 00:31:32 GMT</pubDate></item><item><title><![CDATA[Reply to version sort - feedback on Mon, 12 Oct 2015 02:13:36 GMT]]></title><description><![CDATA[<p>Und wenn irgendwann mal eine Versionsnummer 1.234 kommt, funktioniert es nicht mehr? Ich würde den Teil vor dem Punkt numerisch vergleichen und die Zeichenkette nach dem Komma lexikographisch.</p>
<p>&quot;precision&quot; schreibt man übrigens nicht so, wie du denkst. Und das Wort für Dezimaltrennzeichen ist &quot;decimal&quot; oder &quot;decimal mark&quot;, weil es eben für die meisten Menschen auf der Welt kein &quot;comma&quot; ist. &quot;front&quot; ist das &quot;Vorderteil&quot;, nicht &quot;vor&quot;, welches &quot;before&quot; wäre.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2470706</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2470706</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 12 Oct 2015 02:13:36 GMT</pubDate></item><item><title><![CDATA[Reply to version sort - feedback on Mon, 12 Oct 2015 08:56:39 GMT]]></title><description><![CDATA[<p>Hi SeppJ,</p>
<p>warum soll es mit &quot;1.234&quot; nicht funktionieren?</p>
<p>wie kann ich die zeichen nach dem dezimalzeichen lexikographisch vergleichen?</p>
<p>LG</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2470716</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2470716</guid><dc:creator><![CDATA[coderatte]]></dc:creator><pubDate>Mon, 12 Oct 2015 08:56:39 GMT</pubDate></item><item><title><![CDATA[Reply to version sort - feedback on Mon, 12 Oct 2015 09:19:45 GMT]]></title><description><![CDATA[<p>coderatte schrieb:</p>
<blockquote>
<p>warum soll es mit &quot;1.234&quot; nicht funktionieren</p>
</blockquote>
<p>Weil dein Padding am Ende nur bis 2 Nachkommastellen auffüllt. Das heißt bei 1.234 vergleichst du 234 mit den nur zwei Nachkommastellen der anderen Versionsnummern. Statt den Teil hinter dem Komma als int zu vergleichen müsste der direkte Vergleich der strings funktionieren (ungetestet). Das ist dann wie die Sortierung im Wörterbuch. Also der Nachkommateil &quot;23&quot; kommt vor &quot;234&quot;.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2470720</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2470720</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Mon, 12 Oct 2015 09:19:45 GMT</pubDate></item><item><title><![CDATA[Reply to version sort - feedback on Mon, 12 Oct 2015 10:32:11 GMT]]></title><description><![CDATA[<p>@sebi707: meinst du so?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;sstream&gt;
#include &lt;algorithm&gt;
using namespace std;

int max_precision = 0;

void append_zero(std::string &amp;version, int len) {
	for (int i = 0; i &lt; len; i++) {
		version.push_back('0');
	}
}

std::string get_string_front_comma(std::string &amp;str) {
	auto comma_pos = str.find('.');
	return str.substr(0, comma_pos);
}

std::string get_string_after_comma(std::string &amp;str) {
	auto comma_pos = str.find('.');
	return str.substr(comma_pos, str.size() - comma_pos);
}

int string_to_int(std::string &amp;str) {
	int value = 0;
	std::stringstream ss;
	ss &lt;&lt; str;
	ss &gt;&gt; value;
	return value;
}

void add_zero_padding(std::string &amp;str) {
	auto pos_comma = str.find(&quot;.&quot;);
	if (pos_comma == std::string::npos) {
		throw &quot;invalid version number&quot;;
	}
	int precision = str.size() - pos_comma - 1;
	if (precision &lt; max_precision) {
		append_zero(str, max_precision - precision);
	}
}

void get_max_precision(std::vector&lt;std::string&gt; &amp;vec) {
	for (int i = 0; i &lt; vec.size(); i++) {
		auto pos_comma = vec[i].find(&quot;.&quot;);
		if (pos_comma == std::string::npos) {
			throw &quot;invalid version number&quot;;
		}
		int precision = vec[i].size() - pos_comma - 1;
		if (precision &gt; max_precision) {
			max_precision = precision;
		}
	}
}

struct compare {
	bool operator()(std::string lhs, std::string rhs) {
		add_zero_padding(lhs);
		add_zero_padding(rhs);

		// compare front comma, nummerical compare
		std::string s1 = get_string_front_comma(lhs);
		std::string s2 = get_string_front_comma(rhs);
		int l = string_to_int(s1);
		int r = string_to_int(s2);
		if (l != r) {
			return l &lt; r;
		}

		// compare after comma, lexicographical compare
		return lhs &lt; rhs;
	}
};

void version_sort(std::vector&lt;string&gt; &amp;vec) {
	get_max_precision(vec);
	std::sort(vec.begin(), vec.end(), compare());
}

int main() {
	// your code goes here

	try {
		std::vector&lt;string&gt; versions = {&quot;1.2a&quot;, &quot;1.1&quot;,&quot;1.15&quot;,&quot;12.0&quot;,&quot;1.23&quot;,&quot;12.3&quot;, &quot;1.234&quot;};
		version_sort(versions);

		for (auto e : versions) {
			std::cout &lt;&lt; e &lt;&lt; endl;	
		}
	} catch(const char *e) {
		std::cout &lt;&lt; e &lt;&lt; std::endl;
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2470724</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2470724</guid><dc:creator><![CDATA[coderatte]]></dc:creator><pubDate>Mon, 12 Oct 2015 10:32:11 GMT</pubDate></item><item><title><![CDATA[Reply to version sort - feedback on Mon, 12 Oct 2015 10:51:23 GMT]]></title><description><![CDATA[<p>Ja so ginge es. Und die Geschichte mit get_max_precision und dem Padding wird ja dann unnötig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2470726</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2470726</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Mon, 12 Oct 2015 10:51:23 GMT</pubDate></item><item><title><![CDATA[Reply to version sort - feedback on Mon, 12 Oct 2015 11:10:57 GMT]]></title><description><![CDATA[<p>Grauenhafter Code, daher werde ich den nicht weiter kommentieren. Wenn ich schon sehe, dass eine Stringkonstante geworfen wird <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /> .</p>
<p>Aber zu Versionsnummern habe ich einen Hinweis: Üblicherweise ist die Versionsnummer keine Fliesskommazahl sondern eine Folge von dezimalen Ganzzahlen. Also nach &quot;1.9&quot; kommt &quot;1.10&quot;. Und manchmal gibt es noch eine weitere Stelle wie beispielsweise &quot;1.9.1&quot;, welches am ehesten so zwischen &quot;1.9&quot; und &quot;1.10&quot; einsortiert weder sollten. Wobei das schon nicht ganz eindeutig ist.</p>
<p>Siehe auch <a href="http://wiki.ubuntuusers.de/kernel/linux-versionsnummern" rel="nofollow">Linux-Versionsnummern</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2470727</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2470727</guid><dc:creator><![CDATA[tntnet]]></dc:creator><pubDate>Mon, 12 Oct 2015 11:10:57 GMT</pubDate></item><item><title><![CDATA[Reply to version sort - feedback on Mon, 12 Oct 2015 11:41:40 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/9578">@tntnet</a>: das ist kein production ready code!!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2470730</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2470730</guid><dc:creator><![CDATA[coderatte]]></dc:creator><pubDate>Mon, 12 Oct 2015 11:41:40 GMT</pubDate></item></channel></rss>