Sorting Logic in Common - swap

Hi friends my question looks silly.but still im asking ,i have an array list of 10 elements
int array[]={3,45,28,6,87,45,100,87,95,1}
can any one tel me the logic of sorting this array list in ascending order.im not asking about programetic pattern.just asking the swapping logic generally.please help me

Related

Why is 100 in front of 11 in firebase documents order?

I'm building an app that will display a list of user info, with each one a numeric ID that is stored when creating the profile and used as the docID in firebase. To facilitate the search, I used this method so the ID would be displayed in ascending order. But, after some tests, I notice that, for example, 100 comes first then 11. Why does it happen? Is there a way to correct/prevent it?
I was storing ID as String. But, as the answer suggested, I changed it to both int and double. Still, 11 comes after 100
It's hard to say for certain without seeing your database, but most likely you're storing the numbers as strings. In such cases Firebase (both Firestore and Realtime Database) will sort the values lexicographically, and in that order "100" comes before "11" - just as "baa" comes before "bb".
Why is 100 in front of 11 in firebase documents order?
Because you're sorting alphabetically instead of numerically. 0 comes before 1 in most character encodings like ASCII and Unicode.
After the answers I got and some research I finally got it.
The main problem was that I was storing the ID as String.
After fixing it, I added .orderBy('id') in the place where I retrieve the
uses' list.
Thank you everyone one for the time spent trying to help me.
Another way is to perform sorting after you queried. It is not efficient though, but in case you cannot change the data type or ids themselves:
const query = await db.collection("my-collection").get();
query.docs.sort((doc1, doc2) => parseInt(doc1.id) - parseInt(doc2.id));

In Flutter, how to transform a list of items into a Table widget using the stream API?

In a flutter app, I have a List of 25 items (for example int numbers) which I want to be displayed in a table of 5 rows & 5 columns. I want to use the stream API (map, take, etc.) to build each item using its own index (like in ListView.builder).
I imagine something like:
Table(children: List.generate(25, (i) => i).map((i) => MyTableCell(i)).toList().take(5, into(TableRow()))) but of course this won't do...
I really don't want to use for loops for that purpose . Any ideas?
I hope that I explained myself well. I'll add details if needed. Thank you.
I agree that using a GridView would be easiest. If your really want to use a Table, I guess the challenge is dividing your List<int> of 25 items into a List<List<int>> so that you can use it as children for the Table?
I'd suggest:
yourList.fold([[]], (list, x) {
return list.last.length == 5 ? (list..add([x])) : (list..last.add(x));
});
(Solution shamelessly copied from this post - because it's a great one :-)).
However, some might say that a for loop doing the same thing would be more common and thus easier to read for many... ;-)
A GridView takes a cross-axis count, and a stream of items. No math required.

how to display multiple select list after node submit

I have been reading around and just havent found any type of answer.
I have a multiple select list and need to take the the values of the multiple select and pass them to an API call.
But I dont understand how drupal renders the multiple select list and how to habdle this.
Can someone please explain this to me or if you have come across any docs that explain this, please point me in the right direction.
Also, when I try to write the select to the db, I always get a the value 1... never changes...
Any help is appreciated :)
Solved:
The Multiple select is stored in an associative array where the key value pair are taken from the key value of the list, this means that both the key and value get the same value.
I did this by using devels dvm(); function which displays the array in the message area after the node was saved.

LINQ to SQL grouping and passing onto a view

I am new to Asp.Net, MVC3, Linq, and everything else related to it. I'm very used to PHP and ColdFusion so pardon my ignorance! Essentially what I am trying to do is re-create the situation in ColdFusion where I can perform the equivalent of a cfoutput's group attribute. I'm so used to just calling a stored procedure and then doing
<cfoutput group="id">
and then another inner cfoutput for any columns that have non-distinct data. Works like a charm! Not so much in Asp.Net.
I would like to stay with using my stored procedure, which is returning a join from two tables with a one-to-many relationship. For example's sake let's say I have 3 columns: a full name, a title, and a graduation year. The graduation year is the column from the joined table, so my result from the stored procedure looks like this:
Jim Professor 2005
Jim Professor 2008
Jim Professor 2011
I am sending this to the View. I am assuming it's the View's job to then group the data based on one of the columns (probably the full name). I want to output an HTML table with 3 columns and in this situation I would have ONE row:
Jim Professor 2005, 2008, 2011
I have googled tons of examples that use this thing called a group key. This does not seem to help me because I'm not interested in just outputting one value "Jim" (or whatever the grouped value is), I need both "Jim" and "Professor" values to be output for each row. My thinking is I would need 2 foreach loops, the outer loop displaying the fullname and title and the inner loop going through all possible matches for the graduation years. I cannot seem to get the graduation years in a group, especially with this IGrouping syntax. The key can only store one value and I need every value on that row, I only really need one or two values to be iterated over. Should I try and create a custom view model after I perform a secondary linq grouping and then send that to a strongly typed view?
EDIT:
Ok, I have code that works but it seems very inefficient as I basically have to re-define all of the columns/values that I have from my stored procedure. It almost makes me want to forget stored procedures and just use LINQ for everything. It seems what I was asking for is a kind of "group on multiple columns" and link helped immensely.
var records = db.getRecords();
var groups = from r in records
group r by new
{
r.id
}
into row
select new ListVM()
{
id = row.Key.id,
fullname = row.Select(x => x.fullname).First(),
title = row.Select(x => x.title).First(),
degrees = row.Select(x => x.degree_name).ToList(),
majors = row.Select(x => x.major_name).ToList()
};
return View(groups);
I of course had to create a ViewModel for this to work. In my view then I can use for loops to iterate over the degrees and majors lists. Is this the best way to do this? I just generally need more than just the group key to display my entire row of information, and only want to iterate over lists once or twice in a 20 column row, as opposed to only displaying the group key once and iterating over everything in most examples I see.
I'm not that big specialist with Linq and MVC, but faced with your problem I would:
Deal with data preparation in controller/model, after being taught that view should be concerned with displaying things only.
I would use knowledge from these topics to solve your particular problem:
a) grouping by multiple columns:
Group By Multiple Columns
b) Concatenation as an aggregate function:
Using LINQ to concatenate strings
c) Using aggregates and grouping by multiple columns
How to write a LINQ query combining group by and aggregates?
Once you have data in your view model, just display it.
I believe I've finally found out how to solve what I was looking for. A "group join" seems to solve my problem with ease. The information I found on this page solved it: http://geekswithblogs.net/WillSmith/archive/2008/05/28/linq-joins-and-groupings.aspx

Pulling data into a RadioButtonList including randomizing how it is displayed

I'm new to programming, so bear with me as I try to explain what I"m needing to do. This is in ASP.net iwth VB. I have a quiz project that I've been assigned for work. This quiz project contains two main SQL Databases it pulls the info from.
First is a Quiz table, which contains three columns: QuizID (Pri Key), Title, Description. This is where I declare the quizzes, indicated by the QuizID (1, 2, 3....etc)
The second table is the Questions table, containing the following columns: QuestionsID (Pri Key), Title, Answer1, Answer2, Answer3, Answer4, Answer5, CorrectAnswer, QuestionOrder, QuizID
The QuizID in the Quesitons table matches the QuizID in the Quiz table. Thus for QuizID = 1, it consists of all the questions with the matching QuizID in the Questions table. The CorrectAnswer I want to assign a simple value (1, 2, 3...etc).
I need a way to take a set of questions (based on the QuizID) put them in some sort of table and randomize them (or rather shuffle them) so that each time this quiz is taken, it pulls all questions randomly, but not repeating any)
I then want the code to pull the question and coorepsonding answers to pick from in a radiobuttonlist. However, It only needs to pull the number of answers. Example, a True/False will only have Answer1 and Answer2. The other Answers will have the NULL value in it.
There needs to then be a way to go through each one of these questions that have been placed in the table (in that random order) using a "next" button. When a question and possible answwers are displayed, I alaready have code to keep the answers in the session to grade it at the end (using correctAnswer and selected answer.
I have no specific way to bind the data to the Database. I have used SQLDataSource to make other connections before, but I don't know if this is the best way.
Thanks in advance for any advice. As I said, I'm a noob, so providing all the code will be helpful.
Can you provide any sample code to demonstrate the approach that you wish to take. I am willing to help you learn and fix bugs, but I wont write all your code for you.

Resources